Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have Google maps center around geo-locs and zoom in appropriately

I would like to pass an x amount of geo-locations to the Google Maps API and have it centered around these locations and set the appropriate zoom level so all locations are visible on the map. I.e. show all the markers that are currently on the map.

Is this possible with what the Google Maps API offers by default or do I need to resolve to build this myself?

like image 473
Luke Avatar asked Nov 27 '08 03:11

Luke


People also ask

How do I fix 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.

How do I change the zoom level on Google Maps?

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.

What is zoom level in Google map?

The Google Maps API provides map tiles at various zoom levels for map type imagery. Most roadmap imagery is available from zoom levels 0 to 18, for example. Satellite imagery varies more widely as this imagery is not generated, but directly photographed.


2 Answers

I used fitBounds (API V3) for each point:

  1. Declare the variable.

    var bounds = new google.maps.LatLngBounds();
    
  2. Go through each marker with FOR loop

    for (i = 0; i < markers.length; i++) {
        var latlng = new google.maps.LatLng(markers[i].lat, markers[i].lng);
        bounds.extend(latlng);
    }
    
  3. Finally call

    map.fitBounds(bounds);
    
like image 178
gray Avatar answered Nov 08 '22 18:11

gray


There are a number of ways of doing this but it is pretty easy if you are using the V2 API. See this post for an example, this group post or this post.

like image 38
carson Avatar answered Nov 08 '22 20:11

carson