Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Google Maps API to set the correct zoom level for a country?

Is there a was of automatically setting the zoom level based on the size of the country that the map has been centered on?

maps.google.com does exactly what I need, so, for example, if I search for Russia I get a zoom level such that Russia just fits on screen, and when I search for Cuba I get a higher zoom level so that Cuba just fits.

Is there some way of giving the Maps Api a country location and getting an appropriate zoom level.

If not, I guess that I would have to manually (ugh!) create my own table for this information. Or is this information freely available somewhere?

like image 540
krishnaz Avatar asked Feb 15 '10 09:02

krishnaz


People also ask

How do I fix zoom on Google Maps?

To fix Google maps zooming problems, for Google maps default zoom you want to know how to change the zoom level on Google Maps. You can change the zoom level by going to the Edit map page and then selecting 'default zoom level' in the map information section and then clicking save map.

How do I zoom in Google Maps API?

Users can zoom the map by clicking the zoom controls. They can also zoom and pan by using two-finger movements on the map for touchscreen devices.

How do I custom zoom on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.

Can you customize Google Maps API?

Google Maps allows you to customize maps by adding markers manually or as a group. This method is simple and free, making it a great choice for anyone with limited experience or budget. Click on the Menu icon in the top left-hand corner. Click on the “Your Places” option in the menu.


2 Answers

For V3 this code worked for me:

var geocoder = new google.maps.Geocoder();    geocoder.geocode( { 'address': address}, function(results, status) {       if (status == google.maps.GeocoderStatus.OK) {         map.setCenter(results[0].geometry.location);         map.fitBounds(results[0].geometry.viewport);       }     }); 
like image 175
Mahesh M Avatar answered Sep 18 '22 09:09

Mahesh M


For API v3 check this answer.

You can use the Google Maps Client-side Geocoder to get the bounding box of the country, as in the following example:

// API version 2 var geocoder = new GClientGeocoder();  geocoder.getLocations("Russia", function (locations) {       var north = locations.Placemark[0].ExtendedData.LatLonBox.north;     var south = locations.Placemark[0].ExtendedData.LatLonBox.south;     var east  = locations.Placemark[0].ExtendedData.LatLonBox.east;     var west  = locations.Placemark[0].ExtendedData.LatLonBox.west;      var bounds = new GLatLngBounds(new GLatLng(south, west),                                     new GLatLng(north, east));      map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); });  // API version 3 // ... set north, south, east and west ... var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(south, west),                                            new google.maps.LatLng(north, east)); map.fitBounds(bounds); 

The screenshots below show the results of the above technique when searching for Russia and Cuba:

Zoom on Russia and Cuba

like image 45
Daniel Vassallo Avatar answered Sep 21 '22 09:09

Daniel Vassallo