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;
}
有一个'radius'参数.. - 定义返回结果的距离(以米为单位)。允许的最大半径是50000米。注意,如果指定了rankby = distance(在下面的可选参数下描述),则不得包含半径。检查此链接https://developers.google.com/places/web-service/search – Stallion
我不认为这与Places API中的radius参数有关,上面的代码仅使用Geocoding API,并且不清楚对我来说OP代表“google autocomplete”意味着什么,但是代码没有显示任何使用Places Autocomplete API的痕迹。 – miguev