Google maps API is not using region hint [duplicate]
I'm having problems telling the Google maps API to restrict the results from the geocoding service to a specific region.
A simple example reproducing the problem is here: http://jsfiddle.net/pqZGr/
The JavaScript code is really simple:
$(document).ready(function () {
var geocoder = new google.maps.Geocoder();
$("#name").autocomplete({
source: function (request, response) {
geocoder.geocode({ 'address': request.term, 'region': 'it' }, function (results, status) {
response($.map(results, function (item) {
return {
value: item.formatted_address
}
}));
})
}
});
});
I also specified the API URL as http://maps.google.com/maps/api/js?region=it&lang开发者_开发百科uage=it&sensor=false
besides the 'region': 'it'
in the request.
The problem is the addresses returned by Google are not restricted (nor hinted) to Italy
For example if you search mil
(the begininning of Milano
) I get Circondario di Miltenberg, Germania
and Milion, Alemdar Mh., 34122 Istanbul/Provincia di Istanbul, Turchia
and reka Mil', Sacha-Jacuzia, Russia
.
It's returning results (also, only three?) from all over the world, an nothing from Italy (even when there are multiple cities matching mil
).
What am I doing wrong?
As a side note, is it possible to restrict the search only to the cities, and not to full addresses?
Thanks in advance.
It sounds like you really should be using the Google Places Autocomplete API standalone or built in to the Google Maps API.
The geocoding service isn't designed for autocomplete queries, and won't return the kind of results you're looking for. The region parameter is a hint like you said, but it's not a restriction so if there is no suitable "mil" in Italy, or there is a much better match elsewhere you'll get those results.
There's no way to restrict the results to just cities.
If you are using Google geocoder autocomplete with jQuery UI, you can edit one line in ui.geo_autocomplete.js, line 46, and add your region. It's not 100% precise, but will reduce the number of found addresses:
this.options._geocoder.geocode({'address': _address}, function(_results, _status)
Replace with:
this.options._geocoder.geocode({'address': _address + ' YOUR_REGION_HERE'}, function(_results, _status)
精彩评论