2016-10-04 151 views
0

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

当客户在搜索框中输入他的地址时,我希望它只搜索图卢兹市的所有地址,基于图卢兹市中心,覆盖距离为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; 
} 
+0

有一个'radius'参数.. - 定义返回结果的距离(以米为单位)。允许的最大半径是50000米。注意,如果指定了rankby = distance(在下面的可选参数下描述),则不得包含半径。检查此链接https://developers.google.com/places/web-service/search – Stallion

+0

我不认为这与Places API中的radius参数有关,上面的代码仅使用Geocoding API,并且不清楚对我来说OP代表“google autocomplete”意味着什么,但是代码没有显示任何使用Places Autocomplete API的痕迹。 – miguev

回答

1

你的描述听起来像你正在使用的地图的JavaScript API的地方图书馆Search Box小部件,但你的代码只显示在使用reverse geocoding地理编码API网络服务。

因为我觉得只有前者是有道理的,我来试试看,你会喜欢下面的功能要求,将允许你限制自动完成一个城市:

  • Issue 8606:结合地方限制
  • Issue 4433:允许componentRestrictions以相同的组件滤波器作为地理编码API服务

现在,我知道的选项是:

  • 将搜索框bounds这些城市的(你可以从地理编码API获得)和那些范围内依靠插件更喜欢(不限制)的建议,或
  • 构建使用AutocompleteService.getQueryPredictions()您自己的小工具并且做你自己的事后过滤来删除建议,但这将是非常棘手的,因为城市的名称不一定总会成为建议的一部分。