Reputation: 11989
I'm using the Google Maps API (v2) and would like to center the map onload to a country (for example england).
At the moment i center the map using:
map.setCenter(new GLatLng( 43.907787,-79.359741), 9);
But this obviously requires longitude and Latitude.
Any way to do this by inserting a name of a country?
Upvotes: 14
Views: 30696
Reputation: 2297
You need to geocode the address first:
var geocoder = new google.maps.Geocoder();
var location = "England";
geocoder.geocode( { 'address': location }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
} else {
alert("Could not find location: " + location);
}
});
Upvotes: 10
Reputation: 33239
var country = "United States"
var map = new GMap2($("#map")[0]);
map.setUIToDefault();
var geocoder = new GClientGeocoder();
geocoder.getLatLng(country, function (point) {
if (!point) {
// Handle error
} else {
map.setCenter(point, 8, G_PHYSICAL_MAP);
}
});
Upvotes: 17
Reputation: 51062
Turning a location name or address into a latitude/longitude like this is called geocoding. Google Maps API now includes this capability: see http://code.google.com/apis/maps/documentation/services.html#Geocoding
They include a sample application where you can type in an address, and it does work to simply type a country name. I don't know if they are going to the exact center of the country.
Upvotes: 5