According to the documentation, I found out this : geocoder.setBaseCountryCode(countrycode);
However this did not work even if I hard codded the country code ( both cases :small-capital).
Any help?
According to the documentation, I found out this : geocoder.setBaseCountryCode(countrycode);
However this did not work even if I hard codded the country code ( both cases :small-capital).
Any help?
Get information from this Country database.
Pick the capital-city of the required country and center the map around that.
Just in case if you are looking for SQL file which has ISO country codes (both 2 and 3 chars), then you might also want to look @ http://27.org/isocountrylist/
geocoder.setBaseCountryCode()
will not center the map. That will set the base country for geocoding, so that if you try to geocode 'Plymouth', it will distinguish between the UK and the Massachusetts cities.
If you want to specify the country code manually, you could use the GClientGeocoder
as follows:
var country_code = "ES"; // Change the country code here
function initialize() {
if (GBrowserIsCompatible()) {
var map = null;
var geocoder = null;
map = new GMap2(document.getElementById("map_canvas"));
geocoder = new GClientGeocoder();
if (geocoder) {
geocoder.getLatLng(
"Country: " + country_code,
function(point) {
if (point) {
map.setCenter(point, 13);
}
}
);
}
}
}
There is no need to use set the setBaseCountryCode()
in this case, because country codes are obviously unique.
If you are using version 3 of the Google Maps API, you may want to use the google.loader.ClientLocation
to center the map automatically to the client's country as in this Google Demo. This uses the IP address of the client to determine the country.
The code would look something like this:
function initialize() {
// Initialize default values
var zoom = 3;
var latlng = new google.maps.LatLng(37.4419, -100.1419);
// If ClientLocation was filled in by the loader, use that info instead
if (google.loader.ClientLocation) {
zoom = 13;
latlng = new google.maps.LatLng( google.loader.ClientLocation.latitude,
google.loader.ClientLocation.longitude);
}
var myOptions = {
zoom: zoom,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
}
Note that geolocation from IP addresses is not a very reliable science. You will be getting the location of your ISP, which can be quite far away, and in addition the IP-to-location databases aren't always up to date with the latest changes, so you might not have any data for a particular IP address. However since you would be checking for just the country, the margin of error would be quite small.