Google autocomplete to a specific city

北城以北 提交于 2019-12-13 17:28:27

问题


I already create a geocode api on google console. I have a search box input with the google autocomplete in my site and I would like to set a specific city to my geocode api and integrate it in my web app. The city is: toulouse, Country: France.

When the customer type his address on the search box, I want it to only search all address in the city of toulouse, based on the center of toulouse with covered distance of 20 km.

Here is my code based on the file Function.php

 public function geoCoding($lat='',$lng='')
{                       
    $protocol = isset($_SERVER["https"]) ? 'https' : 'http';
    if ($protocol=="http"){
        $url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng."&sensor=true";
    } else $url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng."&sensor=true";

    $google_geo_api_key=getOptionA('google_geo_api_key');
    if (!empty($google_geo_api_key)){
        $url=$url."&key=".urlencode($google_geo_api_key);
    }

    $data = @file_get_contents($url);
    if (!empty($data)){
        $result = json_decode($data,true);                 
        //dump($result);
        if (!isset($result['results'])){
            return false;
        }
        if (is_array($result['results']) && count($result['results'])>=2){
            $location = array();
             foreach ($result['results'][0]['address_components'] as $component) {
               switch ($component['types']) {
                  case in_array('street_number', $component['types']):
                    $location['street_number'] = $component['long_name'];
                    break;
                  case in_array('route', $component['types']):
                    $location['street'] = $component['long_name'];
                    break;
                  case in_array('neighborhood', $component['types']):
                    $location['street2'] = $component['long_name'];
                    break;  
                  case in_array('sublocality', $component['types']):
                    $location['sublocality'] = $component['long_name'];
                    break;
                  case in_array('locality', $component['types']):
                    $location['locality'] = $component['long_name'];
                    break;
                  case in_array('administrative_area_level_2', $component['types']):
                    $location['admin_2'] = $component['long_name'];
                    break;
                  case in_array('administrative_area_level_1', $component['types']):
                    $location['admin_1'] = $component['long_name'];
                    break;
                  case in_array('postal_code', $component['types']):
                    $location['postal_code'] = $component['long_name'];
                    break;
                  case in_array('country', $component['types']):
                    $location['country'] = $component['long_name'];
                    $location['country_code'] = $component['short_name'];
                    break;
               }
             }                                   
             return $location;
        }
    } 
    return false;
}

回答1:


Your description sounds like you are using the Search Box widget in the Maps JavaScript API's Places library, but your code only shows use of reverse geocoding in the Geocoding API web service.

Since I think only the former makes sense, I'll venture you'll be interested in the following feature requests that would allow you to restrict autocomplete to a city:

  • Issue 8606: places bound restrict
  • Issue 4433: allow componentRestrictions to filter same components as the geocoding API service

For now, the options I know of are:

  • Set the Search box bounds to those of the city (which you can get from Geocoding API) and rely on the widget to prefer (not restrict) suggestions within those bounds, or
  • Build your own widget using AutocompleteService.getQueryPredictions() and do your own after-the-fact filtering to remove suggestions, but that would be very tricky because the name of the city will not necessarily always be part of the suggestion.


来源:https://stackoverflow.com/questions/39849139/google-autocomplete-to-a-specific-city

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!