Google自动填充到特定的城市 [英] Google autocomplete to a specific city

查看:126
本文介绍了Google自动填充到特定的城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当客户在搜索框中输入他的地址时,我希望它只搜索图卢兹市的所有地址,基于图卢兹的中心,覆盖距离为20公里。

这里是我基于文件Function.php的代码
$ b $公共函数geoCoding($ lat ='',$ lng ='')
{
$协议= isset($ _ SERVER [https]) )? 'https':'http';
if($ protocol ==http){
$ url =https://maps.googleapis.com/maps/api/geocode/json?latlng=\".$lat。, $ LNG。 &安培;传感器=真;
} else $ url =https://maps.googleapis.com/maps/api/geocode/json?latlng=\".$lat.\",\".lig.\"&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'];
休息;
case in_array('route',$ component ['types']):
$ location ['street'] = $ component ['long_name'];
休息;
case in_array('neighborhood',$ component ['types']):
$ location ['street2'] = $ component ['long_name'];
休息;
case in_array('sublocality',$ component ['types']):
$ location ['sublocality'] = $ component ['long_name'];
休息;
case in_array('locality',$ component ['types']):
$ location ['locality'] = $ component ['long_name'];
休息;
case in_array('administrative_area_level_2',$ component ['types']):
$ location ['admin_2'] = $ component ['long_name'];
休息;
case in_array('administrative_area_level_1',$ component ['types']):
$ location ['admin_1'] = $ component ['long_name'];
休息;
case in_array('postal_code',$ component ['types']):
$ location ['postal_code'] = $ component ['long_name'];
休息;
case in_array('country',$ component ['types']):
$ location ['country'] = $ component ['long_name'];
$ location ['country_code'] = $ component ['short_name'];
休息;
}
}
return $ location;
}
}
返回false;


解决方案

您的描述听起来像是你使用Maps JavaScript API的Places库中的搜索框小部件,但您的代码仅显示在Geocoding API网络服务中使用反向地理编码

因为我认为只有前者是有道理的,我敢打赌你会对以下功能请求感兴趣,这些功能请求可以让你限制自动完成到一个城市:




  • Issue 8606 :地点绑定限制

  • Issue 4433 :allow componentRestrictio ns筛选与地理编码API服务相同的组件。



现在,我所了解的选项包括:


  • 将搜索框界限设置为城市(您可以从Geocoding API获取)小部件更喜欢(不限制)这些边界内的建议,或
  • 使用 AutocompleteService.getQueryPredictions()并执行自己的事后过滤来移除建议,但这样做会非常棘手,因为城市名称不会必须始终是建议的一部分。


I already create a geocode api on google console. I have a search box input with the google autocomplete in my site and I would like to set a specific city to my geocode api and integrate it in my web app. The city is: toulouse, Country: France.

When the customer type his address on the search box, I want it to only search all address in the city of toulouse, based on the center of toulouse with covered distance of 20 km.

Here is my code based on the file 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;
}

解决方案

Your description sounds like you are using the Search Box widget in the Maps JavaScript API's Places library, but your code only shows use of reverse geocoding in the Geocoding API web service.

Since I think only the former makes sense, I'll venture you'll be interested in the following feature requests that would allow you to restrict autocomplete to a city:

  • Issue 8606: places bound restrict
  • Issue 4433: allow componentRestrictions to filter same components as the geocoding API service

For now, the options I know of are:

  • Set the Search box bounds to those of the city (which you can get from Geocoding API) and rely on the widget to prefer (not restrict) suggestions within those bounds, or
  • Build your own widget using AutocompleteService.getQueryPredictions() and do your own after-the-fact filtering to remove suggestions, but that would be very tricky because the name of the city will not necessarily always be part of the suggestion.

这篇关于Google自动填充到特定的城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆