Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center on a country by name in Google Maps API v3

Tags:

Would someone tell me how to center on a country by name on the Google Maps API v3? I know how to do it in v2, but need to do it in v3.

like image 702
David Avatar asked Dec 10 '10 11:12

David


People also ask

How do I center a Google map?

Please go to your map and drag and drop your map to the position you wish. You can also zoom in your map a little bit with mouse wheel or zoom cotrols on the bottom right corner of the map. Please remember to save your map after any changes. Hope that helps.

Is there a way to display a single country in Google Map?

You can customize your map for a specific country or region in the following ways: Change the default language settings. Specify a region code, which alters the map's behavior based on a given country or territory.

Can you customize Google Maps API?

The Google Maps APIs now support you in creating beautiful styled maps for your Android and iOS apps as well as your website using the same JSON style object.


2 Answers

You could use Geocoding to lookup the Lat/Lng of the country. Take a look at this sample from Google.

Basically you need to do something like this:

var country = "Germany"; var geocoder;  geocoder.geocode( {'address' : country}, function(results, status) {     if (status == google.maps.GeocoderStatus.OK) {         map.setCenter(results[0].geometry.location);     } }); 

In your initialize function you'll need to set the value of the geocoder object, like this:

geocoder = new google.maps.Geocoder(); 

You'd need to workout what an appropriate zoom level would be, and then set that after you have centered the map.

like image 137
kzhen Avatar answered Oct 17 '22 03:10

kzhen


This code working for me:

      let geocoder = new google.maps.Geocoder();       let 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);           }       }); 
like image 30
Ashish Gupta Avatar answered Oct 17 '22 01:10

Ashish Gupta