Php 谷歌自动完成一个特定的城市
我已经在谷歌控制台上创建了一个地理编码api。我有一个搜索框输入和谷歌自动完成在我的网站,我想设置一个特定的城市,我的地理代码api,并将其集成到我的网络应用程序。城市:图卢兹,国家:法国 当客户在搜索框中键入他的地址时,我希望它只搜索图卢兹市的所有地址,基于图卢兹中心,覆盖距离为20公里 下面是我基于Function.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
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服务相同的组件
- 将搜索框
设置为城市的边界(您可以从地理编码API获得),并依靠小部件在这些边界内选择(而不是限制)建议,或者bounds
- 使用构建您自己的小部件,并在事后进行过滤以删除建议,但这将非常棘手,因为城市名称不一定总是建议的一部分