【发布时间】: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