将地点自动填充结果限制在COUNTRY和STATE

时间:2016-12-18 07:00:06

标签: json google-maps autocomplete google-places-api google-places

我目前正在使用此网址将我的结果限制在澳大利亚境内:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=393&components=country:AUtypes=address&key=[my_api_key]

但是我想说我的搜索限制在当前状态让我们说新南威尔士州。

我试过这个: https://maps.googleapis.com/maps/api/place/autocomplete/json?input=393&components=country:AU&state=NSW&types=address&key=[my_api_key]

但是,维多利亚州的物品返回了相同数量的结果,维多利亚州并非来自新南威尔士州。

根据文件:

enter image description here

https://developers.google.com/places/web-service/autocomplete

但这并不表示限制国家。

2 个答案:

答案 0 :(得分:0)

我的解决方案是通过比较状态和国家/地区以及从Places API返回的说明来过滤传入的结果。示例实现如下:

        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
        // Extract the Place descriptions from the results
        resultList = new ArrayList<>(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            if(predsJsonArray.getJSONObject(i) != null){
                String description = predsJsonArray.getJSONObject(i).getString("description");
                if(description != null){
                    if(description.contains(state + ", " + country))
                    resultList.add(new Place(description,
                            predsJsonArray.getJSONObject(i).getString("place_id")));
                }
            }
        }

答案 1 :(得分:0)

嘿朋友!

我遇到了同样的问题,最终用javascript构建了一个解决方案,能够根据您的需要限制地址。

问题

真正的现实是,Google API自动填充方法没有为我们提供完整的限制地址选项,仅针对所需的国家/地区和边界(如果有坐标的话)。

对于用户来说,每次需要输入正确的地址(至少在我使用的情况下)(消防员紧急呼叫出勤)时,键入所有完整地址(邻居,城市,州等)仍然有些棘手。

解决方案

并非100%不会返回不良结果,但可以节省用户打字的麻烦。

基本上,该脚本会在请求进入Google服务器之前在幕后在请求中添加必要的限制,从而欺骗Google API和用户。

它使用service.getPlacePredictions和google.maps.places.AutocompleteService,并且以记帐方式表示“无地点详细信息的自动填充-每个会话”的方式实现,这意味着从用户开始起,您只需支付一次费用键入,直到他单击地址预测(可用的更便宜的选项)。

使用自定义自动完成功能(而不是Google默认窗口小部件)的另一个优点是,如果API密钥存在问题(例如超出限制),它将不会阻止文本输入。因此,用户可以输入地址,以后系统adms可以对未验证的地址进行正确的处理。

需要在console.cloud.google.com上激活Places API和Maps JavaScript API。

好吧,下面是使用该组件所需要做的一切。

<html>
   <head>
      <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
      <meta content="utf-8" http-equiv="encoding">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" type="text/css" href="style.css">
   </head>
   <body>
      <p>This example uses service.getPlacePredictions and google.maps.places.AutocompleteService what means that the billing stands for "Autocomplete without Places Details - Per Session"</p>
      <!--Make sure the form has the autocomplete function switched off:-->
      <form autocomplete="off" action="/action_page.php">
         <div class="autocomplete" style="width:300px;">
            <p><input id="estado" type="text" name="estado" placeholder="Estado" value="SC"></p>
            <p><input id="cidade" type="text" name="cidade" placeholder="Cidade"value="FLORIANOPOLIS"></p>
            <p><input id="bairro" type="text" name="bairro" placeholder="Bairro" value="CENTRO"></p>
            <p><input id="numero" type="text" name="numero" placeholder="Numero" value="10"></p>
            <input id="logradouro" type="text" name="logradouro" placeholder="Logradouro">
         </div>
         <input type="submit">
      </form>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
      <script async defer src="https://maps.googleapis.com/maps/api/js?key=GOOGLE_API_KEY&libraries=places" type="text/javascript"></script>
      <script src="./autocomplete.js" type="text/javascript"></script>
      <script type="text/javascript">
         let autocomplete = new RestrictedAutocomplete(document.getElementById("logradouro"), function () {
            console.log(this.lastResultBusca);
            console.log(this.lastResultEnderecoCompleto);
            console.log(this.lastResultNumero);
            console.log(this.lastResultNumeroA);
            console.log(this.lastResultLogradouro);
            console.log(this.lastResultLogradouroA);
            console.log(this.lastResultBairro);
            console.log(this.lastResultBairroA);
            console.log(this.lastResultCidade);
            console.log(this.lastResultCidadeA);
            console.log(this.lastResultEstado);
            console.log(this.lastResultEstadoA);
            console.log(this.lastResultPais);
            console.log(this.lastResultPaisA);
            console.log(this.lastResultCEP);
            console.log(this.lastResultCEPA);
            console.log(this.lastResultId);
            console.log(this.lastResultLatitude);
            console.log(this.lastResultLongitude);
         });
         autocomplete.setRestrictions(document.getElementById("estado"), 
            document.getElementById("cidade"), 
            document.getElementById("bairro"), 
            document.getElementById("numero"));
      </script>
   </body>
</html>

自动完成工作:

autocomplete working

选择地址后自动完成: an address has been selected

您可以在GitHub project上看到更多详细信息。

希望有帮助!