【问题标题】:Google autocomplete to a specific cityGoogle 自动填充到特定城市
【发布时间】:2017-02-12 10:23:37
【问题描述】:

我已经在谷歌控制台上创建了一个地理编码 API。我的网站中有一个带有谷歌自动完成功能的搜索框输入,我想为我的地理编码 api 设置一个特定的城市并将其集成到我的网络应用程序中。城市是:图卢兹,国家:法国。

当客户在搜索框中输入他的地址时,我希望它只搜索图卢兹市内的所有地址,基于图卢兹的中心,覆盖距离为 20 公里。

这是我基于文件 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;
}

【问题讨论】:

  • 有一个“半径”参数.. - 定义返回位置结果的距离(以米为单位)。最大允许半径为 50 000 米。请注意,如果指定了 rankby=distance(在下面的可选参数中描述),则不得包含半径。检查此链接developers.google.com/places/web-service/search
  • 我不认为这与 Places API 中的 radius 参数有关,上面的代码只使用了 Geocoding API,我不清楚 OP 与“google autocomplete”是什么意思,但是代码没有显示任何使用 Places Autocomplete API 的痕迹。

标签: php google-maps autocomplete google-geocoding-api


【解决方案1】:

您的描述听起来像是在使用 Maps JavaScript API 的 Places 库中的 Search Box 小部件,但您的代码仅显示在 Geocoding API 网络服务中使用了 reverse geocoding

由于我认为只有前者才有意义,我敢打赌您会对以下允许您将自动完成功能限制在城市范围内的功能请求感兴趣:

  • Issue 8606: 地方限制
  • Issue 4433:允许 componentRestrictions 过滤与地理编码 API 服务相同的组件

目前,我知道的选项是:

  • 将搜索框 bounds 设置为城市的搜索框(您可以从 Geocoding API 获得)并依靠小部件在这些范围内优先选择(而不是限制)建议,或者
  • 使用 AutocompleteService.getQueryPredictions() 构建您自己的小部件并执行您自己的事后过滤以删除建议,但这会非常棘手,因为城市名称不一定总是建议的一部分。

【讨论】:

    猜你喜欢
    • 2013-05-07
    • 2012-01-27
    • 1970-01-01
    • 2023-03-04
    • 2015-10-10
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    相关资源
    最近更新 更多