我一直在寻找的例子:限制谷歌地图自动完成对英国的地址只
http://code.google.com/apis/maps/documentation/javascript/examples/places-autocomplete.html
,并已决定将其纳入我的网站。
是否可以将地址限制为英国地址?
我一直在寻找的例子:限制谷歌地图自动完成对英国的地址只
http://code.google.com/apis/maps/documentation/javascript/examples/places-autocomplete.html
,并已决定将其纳入我的网站。
是否可以将地址限制为英国地址?
试试这个:
var input = document.getElementById('searchTextField');
var options = {
types: ['(cities)'],
componentRestrictions: {country: 'tr'}//Turkey only
};
var autocomplete = new google.maps.places.Autocomplete(input,options);
尽管系统中有一项功能请求可以这样做,但您可以对结果设置“偏差”,但不能严格限制其找到的位置。它作为参数传递给自动完成方法,作为Google地图边界对象。那么自动完成将有利于这些边界内的位置。但是请注意,由于这不是一个硬边界,因此如果边界外的搜索匹配,它将返回那些边界。
从我的用法来看,它似乎有点bug,可以使用一些改进 - 特别是考虑到边界外的任何内容都没有被邻近标记标记,所以边界外的一个区块可能显示为1000英里外面,所以要确保你玩弄正确的界限。
正确答案是使用componentRestrictions,它允许您限制自动填充到国家,如Ercan的答案中所示。 –
正确。它已于2012年7月25日添加到API v3.9中。截至撰写上述答案时,API在v3.6。这是Google对上述功能请求的回答。截至今天,v3.12是最低版本,因此这应该是任何API请求的标准。 –
你会这样做的最好方法是查询自己的位置API并将查询的字符串附加到你的国家。或者,当然,使用geo-autocomplete jQuery plugin。
詹姆斯·阿尔迪是正确的:
http://code.google.com/apis/maps/documentation/javascript/places.html#places_autocomplete
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(49.00, -13.00),
new google.maps.LatLng(60.00, 3.00));
var acOptions = {
bounds: defaultBounds,
types: ['geocode']
};
它是作为搜索达勒姆给北卡罗来纳州达勒姆的第二个结果有点让人讨厌,不管你如何努力将其劝地区偏见 - 你可以将它设置为视口地图边界,它仍然会尝试建议NC状态...... jQuery解决方案可以在这里找到,但似乎没有给出与v3 API一样多的结果。 http://code.google.com/p/geo-autocomplete/
可以拦截JSONP结果由google.maps.places.Autocomplete功能恢复,并利用它们,你认为合适,如按国家来限制和显示结果。
基本上,您重新定义head元素上的appendChild方法,然后监视Google自动填充代码插入到JSONP的DOM中的JavaScript元素。随着JavaScript元素的添加,您可以重写Google定义的JSONP回调函数,以访问原始自动完成数据。
这是一个黑客攻击的一位,在这里去(我使用jQuery,但它没有必要为这个破解工作):
//The head element, where the Google Autocomplete code will insert a tag
//for a javascript file.
var head = $('head')[0];
//The name of the method the Autocomplete code uses to insert the tag.
var method = 'appendChild';
//The method we will be overriding.
var originalMethod = head[method];
head[method] = function() {
if (arguments[0] && arguments[0].src && arguments[0].src.match(/GetPredictions/)) { //Check that the element is a javascript tag being inserted by Google.
var callbackMatchObject = (/callback=([^&]+)&|$/).exec(arguments[0].src); //Regex to extract the name of the callback method that the JSONP will call.
var searchTermMatchObject = (/\?1s([^&]+)&/).exec(arguments[0].src); //Regex to extract the search term that was entered by the user.
var searchTerm = unescape(searchTermMatchObject[1]);
if (callbackMatchObject && searchTermMatchObject) {
var names = callbackMatchObject[1].split('.'); //The JSONP callback method is in the form "abc.def" and each time has a different random name.
var originalCallback = names[0] && names[1] && window[names[0]] && window[names[0]][names[1]]; //Store the original callback method.
if (originalCallback) {
var newCallback = function() { //Define your own JSONP callback
if (arguments[0] && arguments[0][3]) {
var data = arguments[0][4]; //Your autocomplete results
//SUCCESS! - Limit results here and do something with them, such as displaying them in an autocomplete dropdown.
}
}
//Add copy all the attributes of the old callback function to the new callback function. This prevents the autocomplete functionality from throwing an error.
for (name in originalCallback) {
newCallback[name] = originalCallback[name];
}
window[names[0]][names[1]] = newCallback; //Override the JSONP callback
}
}
//Insert the element into the dom, regardless of whether it was being inserted by Google.
return originalMethod.apply(this, arguments);
};
你的方法似乎非常有趣,也是唯一一个解决限制使用Google自己参数的区域的固有问题的方法。你能发布一个完整的工作JSFiddle或类似的纳入上述请?我很难理解它如何适用于Google的示例,并且一个完整的工作示例将非常有帮助。谢谢 –
只要改变谷歌域的地图到您的国家域名,它会自动搜索您的国家内只:
因此: http://maps.google.com/maps/api/geocode/xml?address= {0} &传感器=假&语言= EN
要: http://maps.google.nl/maps/api/geocode/xml?address= {0} &传感器=假&语言= NL
尝试这样的事情。
// Change Bangalore, India to your cities boundary.
var bangaloreBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(12.864162, 77.438610),
new google.maps.LatLng(13.139807, 77.711895));
var autocomplete = new google.maps.places.Autocomplete(this, {
bounds: bangaloreBounds,
strictBounds: true,
});
autocomplete.addListener('place_changed', function() {
});
这是正确的答案 –
componentRestrictions:{country:'gb'} //只会根据问题 – geedubb
限制英国,它应该是'types:['cities']',而不是'types:[' )']' – tirdadc