Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL);
- if(!isset($_POST['lat'])) {
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Поиск места</title>
- </head>
- <body>
- <form action="index.php" method="post">
- <div>
- <div>Широта</div>
- <input type="text" name="lat" placeholder="Широта" value="50.390687">
- </div>
- <br>
- <div>
- <div>Долгота</div>
- <input type="text" name="lng" placeholder="Долгота" value="30.369191">
- </div>
- <br>
- <div>
- <div>
- Радиус поиска мест вокруг (в км)
- </div>
- <input type="text" name="radius" placeholder="Радиус поиска мест вокруг (в км)" value="10">
- </div>
- <br>
- <div>
- <div>Список типов мест, разделяются так: место1|метсо2|место3</div>
- <div>Список возможных смотреть тут: <a href="https://developers.google.com/places/supported_types">https://developers.google.com/places/supported_types</a></div>
- <input type="text" name="places_types" value="food|park|bank|cafe|zoo" placeholder="food|park|bank|cafe|zoo">
- </div>
- <br><br>
- <input type="submit" name="name" value="Найти">
- </form>
- </body>
- </html>
- <?php
- die;
- }
- // Координаты
- $lat = $_POST['lat'];
- $lng = $_POST['lng'];
- // Радиус в метрах
- $radius = $_POST['radius'] * 1000;
- // Максимальное кол-во мест, которые надо получить, кратное 20
- // Минимальное - 20, максимальное, судя по всему, 60
- $count_of_places = 60;
- // Типы мест, разделять с помощью |
- // Смотреть доступные тут https://developers.google.com/places/supported_types
- $places_types = $_POST['places_types'];
- // API Key
- $key = 'AIzaSyAt18jJxksfNsFNalEZgeDNS8qBwO9GdLY';
- // Массив, который будет хранить данные о месте, которое ищем
- $place = array();
- // Первый запрос (получение страны и города), Гугл
- $first_url = 'https://maps.googleapis.com/maps/api/geocode/json?';
- $first_url .= '&latlng=' . $lat . ',' . $lng;
- $first_url .= '&language=ru';
- $first_url .= '&result_type=country|postal_code|street_address';
- $first_url .= '&key=' . $key;
- $ch = curl_init();
- if (!$ch) {
- die("Couldn't initialize a cURL handle");
- }
- curl_setopt($ch, CURLOPT_URL, $first_url);
- curl_setopt($ch, CURLOPT_HTTPGET, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Accept: application/json'
- ));
- $result = curl_exec($ch);
- curl_close($ch);
- $result = json_decode($result, true);
- $result = $result['results'][0]['address_components'];
- foreach ($result as $value) {
- $types = $value['types'];
- if(in_array('street_number', $types)) {
- $place['street_number'] = $value['long_name'];
- }
- if(in_array('route', $types)) {
- $place['street'] = $value['long_name'];
- }
- if(in_array('locality', $types)) {
- $place['city'] = $value['long_name'];
- }
- if(in_array('administrative_area_level_2', $types)) {
- $place['district'] = $value['long_name'];
- }
- if(in_array('administrative_area_level_1', $types)) {
- $place['region'] = $value['long_name'];
- }
- if(in_array('country', $types)) {
- $place['country'] = $value['long_name'];
- }
- }
- //
- // echo "<pre>";
- // print_r($result);die;
- // $place = array(
- // 'country' => $result[4]['long_name'],
- // 'city' => $result[1]['long_name'],
- // );
- // Второй запрос
- $ch = curl_init();
- $second_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?';
- $next_page_url = $second_url .= '&key=' . $key;
- $second_url .= '&language=ru';
- $second_url .= '&location=' . $lat . ',' . $lng;
- $second_url .= '&radius=' . $radius;
- $second_url .= '&types=' . $places_types;
- if (!$ch) {
- die("Couldn't initialize a cURL handle");
- }
- curl_setopt($ch, CURLOPT_URL, $second_url);
- curl_setopt($ch, CURLOPT_HTTPGET, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Accept: application/json'
- ));
- $result = json_decode(curl_exec($ch), true);
- curl_close($ch);
- $pagetoken = $result['next_page_token'];
- $result = $result['results'];
- // Список мест, которые получили
- $places = array();
- foreach ($result as $key => $value) {
- $places[] = array(
- 'name' => $value['name'],
- 'icon' => $value['icon'],
- 'coords' => $value['geometry']['location'],
- 'types' => $value['types'],
- );
- }
- $count_of_places /= 20;
- $count_of_places--;
- if ($count_of_places > 0) {
- sleep(2);
- for($k=0; $k < $count_of_places; $k++) {
- $ch = curl_init();
- if (!$ch) {
- die("Couldn't initialize a cURL handle");
- }
- $url = $next_page_url . '&pagetoken=' . $pagetoken;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HTTPGET, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Accept: application/json'
- ));
- $result = json_decode(curl_exec($ch), true);
- curl_close($ch);
- foreach ($result['results'] as $key => $value) {
- $places[] = array(
- 'name' => $value['name'],
- 'icon' => $value['icon'],
- 'coords' => $value['geometry']['location'],
- 'types' => $value['types'],
- );
- }
- if(isset($result['next_page_token']) && $result['next_page_token']) {
- $pagetoken = $result['next_page_token'];
- sleep(2);
- } else {
- break;
- }
- }
- }
- echo "Найденное место: ";
- echo "<pre>";
- print_r($place);
- echo "</pre><br><hr><br>";
- echo "Места в радиусе " . $radius/1000 . "км";
- echo "<pre>";
- print_r($places);
- echo "</pre>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement