Reputation: 12487
I can't seem to get this right at the moment, Google talks about region basing here:
https://developers.google.com/maps/documentation/geocoding/#RegionCodes
It uses the following parameter:
region:
This is the code im working with: http://jsfiddle.net/spadez/Jfdbz/19/
My question is how do I pass my variable "ctryiso" to this parameter in my script? When I try, nothing changes, so when ctryiso is set to US and I type in London it still geocodes London, England. I've heard it may be a bit unreliable but still I don't think my implementation is correct.
Upvotes: 6
Views: 742
Reputation: 3353
function getLatLong(address){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
I am unable to understand your exact need but, I hope it will help you and if not then please tell me your exact need in short way
Upvotes: 0
Reputation: 4779
As noted in a different answer, you can include the region
in your geocode request to bias (not restrict) the result.
However, in this particular case, London in the UK is still considered a better result than London in the US. Therefore you can additionally restrict (vs bias) the result using the GeocoderComponentRestrictions
as described in the Geocoding Service docs
I have made a fiddle showing this type of filtering by requiring the results are in the US.
You'll note that the result is in London, USA, but if you remove the GeocoderComponentRestrictions
from the fiddle code, the first result is now in London, despite the region bias.
However it's still important to note that biasing does work. For example, if you now change the region to 'ES' and the address to 'Toledo', the result is in Spain. If you remove the bias, it defaults to Toledo USA.
tl;dr use region
to bias, and the GeocoderComponentRestrictions
to restrict.
Upvotes: 0
Reputation: 13639
You can try adding the region parameter as a query string when you
load google map APIs, although by default it should be US
https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false®ion=US
You can also append ",USA" to the address you pass to the geocoder - that works really well but it is ugly.
You could also return all the results and parse them and display only ones that are in the country of interest. In most cases you will have to do that anyhow as there are multiple locations with the same name in most countries .... This is the preferred method as if someone types in London, USA you will get London, KY and London,OH and the user/you still needs to decide which one it is they want...
Upvotes: 0
Reputation: 672
Normally where you have:
geocoder.geocode({
address: query
}
You should pass region
just like that (as you already mentioned):
geocoder.geocode({
address: query,
region: SOME_VARIABLE_WITH_REGION_CODE
}
And it should work, although... it seems it isn't and more people are having the same problem.
The (ugly) solution is to add it to address, like this:
geocoder.geocode({
address: query + ', ' + SOME_VARIABLE_WITH_REGION_CODE
}
I've just tested it and it works, but as i said - it's not the "nice" neither correct way to do it - it's just workaround.
As we can see here: https://developers.google.com/maps/documentation/javascript/reference?hl=th-TH&csw=1#GeocoderRequest
You can pass address
, bounds
, location
and region
. Since region doesn't seems to be working, i'd try to use bounds
by first getting coordinates for desired region:
https://developers.google.com/maps/documentation/javascript/reference?hl=th-TH&csw=1#LatLngBounds
Upvotes: 2