如何使用JavaScript获取基于城市阵列的自动完成列表 [英] How to get the autocomplete list based on array of cities using Javascript

查看:116
本文介绍了如何使用JavaScript获取基于城市阵列的自动完成列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Java中的Google Place API获取基于某个城市的自动填充值.但是根据我的代码,它无法正常工作.

I need to get the autocomplete values based on some city using google place API in Javascript. But as per my code its not working as expected.

在这里,我在数组中有一些城市,例如city=['Bhubaneswar','Balesore','abcd'],当用户键入要获取地点的信息时,匹配项应仅属于这些城市.我在下面解释我的代码.

Here I have some cities in array like city=['Bhubaneswar','Balesore','abcd'] and When user will type to get the place the matching should be belongs to these cities only. I am explaining my code below.

<script src="https://maps.googleapis.com/maps/api/js?key=MAP_KEY&libraries=places&callback=initMap" type="text/javascript" async defer></script> 
function initMap() {
      var city=['Bhubaneswar','Balesore','abcd'];
      var input = document.getElementById('pick_addr');
      var options = {
               types: ['(cities)'],
               componentRestrictions: {country: "IN"}
      };
      var autocomplete = new google.maps.places.Autocomplete(input,options);
}

根据我的代码,它只适用于一个国家/地区,但我只需要基于每个城市.

As per my code its coming for one country but I need only per city based.

推荐答案

当前无法将地方信息自动填充"结果限制为特定的城市/地区.作为暂时的解决方法,您可以按照此

It is not currently possible to restrict Places Autocomplete results to specific cities/localities. As a workaround for now, you can bias the autocomplete results to favor an approximate location or area as stated in this documentation.

还有一个现有的功能请求需要过滤 Places API自动完成中的路线,地点,行政区,邮政编码和国家/地区.我建议您对它加注星标"以接收更新. https://issuetracker.google.com/issues/35822067

There is also an existing feature request to filter using route, locality, administrative_area, postal_code and country in the Places API autocomplete. I'd recommend you "star" it to receive updates. https://issuetracker.google.com/issues/35822067

希望这会有所帮助!

这篇关于如何使用JavaScript获取基于城市阵列的自动完成列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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