我试图添加一个谷歌地理代码自动完成字段到我的表单。
我已经能够让它限制结果返回国家(非盟),但如果我可以更好,如果我可以限制结果,以及国家。(新南威尔士/新南威尔士州)
这是我当前的initialize()函数:
function initialize() {
var options = {
types: ['geocode'],
componentRestrictions: {country:"AU"}
};
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),options);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress();
});
}我已经阅读了文档并发现您可以使用types参数来限制结果,但是我无法确定确切的代码应该是什么。
只需将其更改为:
types: ['administrative_area1':'NSW'], // OR 'New South Wales'各种类似于以上(=代替: etc)的组合都没有起作用。
有人能给我指出正确的方向吗?对于我想要达到的实际语法是什么?
谢谢。
发布于 2015-11-24 03:05:19
我使用自动完成,没有任何类型的过滤器,但自动附加我感兴趣的状态到所有用户输入,然后发送查询。
例如,对于想要加州结果的人来说,如果他们要搜索Sacramento,就会自动更正到Sacramento, California的输入,这对我来说有些不错。您可以更具体地处理您发送的数据,例如Sacramento, California, USA。
发布于 2018-04-04 01:05:28
在您的应用程序中保存一个状态lat/长边界的列表,并限制它们的自动完成。
var stateBounds={
ca: ["32.812736", "-119.216815", "34.608128", "-117.039301"]
// lat/long boundaries list for states/provinces.
};
function getStateBounds(state) {
return new google.maps.LatLngBounds(
new google.maps.LatLng(stateBounds[state][0],
stateBounds[state][1]),
new google.maps.LatLng(stateBounds[state][2],
stateBounds[state][3])
);
}
new google.maps.places.Autocomplete(document.getElementById('search'), {
types: ['address'],
componentRestrictions: {country: 'us'},
bounds: getStateBounds('ca') // get LatLngBounds for ca.
});https://stackoverflow.com/questions/21871190
复制相似问题