Advertisement
zlodes

Untitled

Aug 25th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.17 KB | None | 0 0
  1. <?php
  2.  
  3.   error_reporting(E_ALL);
  4.  
  5.   if(!isset($_POST['lat'])) {
  6.   ?>
  7.   <!DOCTYPE html>
  8.   <html>
  9.     <head>
  10.       <meta charset="utf-8">
  11.       <title>Поиск места</title>
  12.     </head>
  13.     <body>
  14.  
  15.       <form action="index.php" method="post">
  16.         <div>
  17.           <div>Широта</div>
  18.           <input type="text" name="lat" placeholder="Широта" value="50.390687">
  19.         </div>
  20.         <br>
  21.         <div>
  22.           <div>Долгота</div>
  23.           <input type="text" name="lng" placeholder="Долгота" value="30.369191">
  24.         </div>
  25.         <br>
  26.         <div>
  27.           <div>
  28.             Радиус поиска мест вокруг (в км)
  29.           </div>
  30.           <input type="text" name="radius" placeholder="Радиус поиска мест вокруг (в км)" value="10">
  31.         </div>
  32.         <br>
  33.         <div>
  34.           <div>Список типов мест, разделяются так: место1|метсо2|место3</div>
  35.           <div>Список возможных смотреть тут: <a href="https://developers.google.com/places/supported_types">https://developers.google.com/places/supported_types</a></div>
  36.  
  37.           <input type="text" name="places_types" value="food|park|bank|cafe|zoo" placeholder="food|park|bank|cafe|zoo">
  38.         </div>
  39.         <br><br>
  40.         <input type="submit" name="name" value="Найти">
  41.  
  42.       </form>
  43.  
  44.     </body>
  45.   </html>
  46.  
  47. <?php
  48.  
  49. die;
  50. }
  51.  
  52. // Координаты
  53. $lat = $_POST['lat'];
  54. $lng = $_POST['lng'];
  55.  
  56. // Радиус в метрах
  57. $radius = $_POST['radius'] * 1000;
  58.  
  59. // Максимальное кол-во мест, которые надо получить, кратное 20
  60. // Минимальное - 20, максимальное, судя по всему, 60
  61. $count_of_places = 60;
  62.  
  63. // Типы мест, разделять с помощью |
  64. // Смотреть доступные тут https://developers.google.com/places/supported_types
  65. $places_types = $_POST['places_types'];
  66.  
  67. // API Key
  68. $key = 'AIzaSyAt18jJxksfNsFNalEZgeDNS8qBwO9GdLY';
  69.  
  70. // Массив, который будет хранить данные о месте, которое ищем
  71. $place = array();
  72.  
  73. // Первый запрос (получение страны и города), Гугл
  74. $first_url = 'https://maps.googleapis.com/maps/api/geocode/json?';
  75. $first_url .= '&latlng=' . $lat . ',' . $lng;
  76. $first_url .= '&language=ru';
  77. $first_url .= '&result_type=country|postal_code|street_address';
  78. $first_url .= '&key=' . $key;
  79.  
  80. $ch = curl_init();
  81. if (!$ch) {
  82.     die("Couldn't initialize a cURL handle");
  83. }
  84. curl_setopt($ch, CURLOPT_URL, $first_url);
  85. curl_setopt($ch, CURLOPT_HTTPGET, true);
  86. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  87. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  88.     'Content-Type: application/json',
  89.     'Accept: application/json'
  90. ));
  91.  
  92. $result = curl_exec($ch);
  93. curl_close($ch);
  94.  
  95. $result = json_decode($result, true);
  96. $result = $result['results'][0]['address_components'];
  97.  
  98.  
  99. foreach ($result as $value) {
  100.   $types = $value['types'];
  101.   if(in_array('street_number', $types)) {
  102.     $place['street_number'] = $value['long_name'];
  103.   }
  104.   if(in_array('route', $types)) {
  105.     $place['street'] = $value['long_name'];
  106.   }
  107.   if(in_array('locality', $types)) {
  108.     $place['city'] = $value['long_name'];
  109.   }
  110.   if(in_array('administrative_area_level_2', $types)) {
  111.     $place['district'] = $value['long_name'];
  112.   }
  113.   if(in_array('administrative_area_level_1', $types)) {
  114.     $place['region'] = $value['long_name'];
  115.   }
  116.   if(in_array('country', $types)) {
  117.     $place['country'] = $value['long_name'];
  118.   }
  119. }
  120.  
  121. //
  122. // echo "<pre>";
  123. // print_r($result);die;
  124.  
  125. // $place = array(
  126. //   'country' => $result[4]['long_name'],
  127. //   'city' => $result[1]['long_name'],
  128. // );
  129.  
  130.  
  131. // Второй запрос
  132. $ch = curl_init();
  133.  
  134. $second_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?';
  135. $next_page_url = $second_url .= '&key=' . $key;
  136. $second_url .= '&language=ru';
  137. $second_url .= '&location=' . $lat . ',' . $lng;
  138. $second_url .= '&radius=' . $radius;
  139. $second_url .= '&types=' . $places_types;
  140.  
  141. if (!$ch) {
  142.     die("Couldn't initialize a cURL handle");
  143. }
  144. curl_setopt($ch, CURLOPT_URL, $second_url);
  145. curl_setopt($ch, CURLOPT_HTTPGET, true);
  146. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  147. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  148.     'Content-Type: application/json',
  149.     'Accept: application/json'
  150. ));
  151.  
  152. $result = json_decode(curl_exec($ch), true);
  153. curl_close($ch);
  154.  
  155. $pagetoken = $result['next_page_token'];
  156. $result = $result['results'];
  157.  
  158. // Список мест, которые получили
  159. $places = array();
  160.  
  161. foreach ($result as $key => $value) {
  162.   $places[] = array(
  163.     'name' => $value['name'],
  164.     'icon' => $value['icon'],
  165.     'coords' => $value['geometry']['location'],
  166.     'types' => $value['types'],
  167.   );
  168. }
  169.  
  170. $count_of_places /= 20;
  171. $count_of_places--;
  172.  
  173. if ($count_of_places > 0) {
  174.   sleep(2);
  175.   for($k=0; $k < $count_of_places; $k++) {
  176.     $ch = curl_init();
  177.     if (!$ch) {
  178.         die("Couldn't initialize a cURL handle");
  179.     }
  180.     $url = $next_page_url . '&pagetoken=' . $pagetoken;
  181.     curl_setopt($ch, CURLOPT_URL, $url);
  182.     curl_setopt($ch, CURLOPT_HTTPGET, true);
  183.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  184.     curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  185.         'Content-Type: application/json',
  186.         'Accept: application/json'
  187.     ));
  188.  
  189.     $result = json_decode(curl_exec($ch), true);
  190.     curl_close($ch);
  191.  
  192.     foreach ($result['results'] as $key => $value) {
  193.       $places[] = array(
  194.         'name' => $value['name'],
  195.         'icon' => $value['icon'],
  196.         'coords' => $value['geometry']['location'],
  197.         'types' => $value['types'],
  198.       );
  199.     }
  200.  
  201.     if(isset($result['next_page_token']) && $result['next_page_token']) {
  202.       $pagetoken = $result['next_page_token'];
  203.       sleep(2);
  204.     } else {
  205.       break;
  206.     }
  207.   }
  208. }
  209.  
  210.  
  211. echo "Найденное место: ";
  212. echo "<pre>";
  213. print_r($place);
  214. echo "</pre><br><hr><br>";
  215.  
  216. echo "Места в радиусе " . $radius/1000 . "км";
  217. echo "<pre>";
  218. print_r($places);
  219. echo "</pre>";
  220.  
  221.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement