Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 谷歌自动完成一个特定的城市_Php_Google Maps_Autocomplete_Google Geocoding Api - Fatal编程技术网

Php 谷歌自动完成一个特定的城市

Php 谷歌自动完成一个特定的城市,php,google-maps,autocomplete,google-geocoding-api,Php,Google Maps,Autocomplete,Google Geocoding Api,我已经在谷歌控制台上创建了一个地理编码api。我有一个搜索框输入和谷歌自动完成在我的网站,我想设置一个特定的城市,我的地理代码api,并将其集成到我的网络应用程序。城市:图卢兹,国家:法国 当客户在搜索框中键入他的地址时,我希望它只搜索图卢兹市的所有地址,基于图卢兹中心,覆盖距离为20公里 下面是我基于Function.php文件的代码 public function geoCoding($lat='',$lng='') { $protoc

我已经在谷歌控制台上创建了一个地理编码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;
}

您的描述听起来像是在使用Maps JavaScript API的Places库中的小部件,但您的代码仅显示在地理编码API web服务中的使用

因为我认为只有前者才有意义,所以我敢说,您可能会对以下功能请求感兴趣,这些请求允许您将自动完成限制在某个城市:

  • :地点限制
  • :允许componentRestrictions筛选与地理编码API服务相同的组件
目前,我所知道的选择有:

  • 将搜索框
    bounds
    设置为城市的边界(您可以从地理编码API获得),并依靠小部件在这些边界内选择(而不是限制)建议,或者
  • 使用构建您自己的小部件,并在事后进行过滤以删除建议,但这将非常棘手,因为城市名称不一定总是建议的一部分

有一个“半径”参数..-定义返回放置结果的距离(以米为单位)。最大允许半径为50 1000米。请注意,如果指定了rankby=距离(在下面的可选参数中描述),则不得包括半径。检查此链接我认为这与Places API中的radius参数无关,上面的代码仅使用地理编码API,我不清楚OP对“google autocomplete”的含义,但代码没有显示任何使用Places autocomplete API的痕迹。