Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center/Set Zoom of Map to cover all visible Markers?

I am setting multiple markers on my map and I can set statically the zoom levels and the center but what I want is, to cover all the markers and zoom as much as possible having all markets visible

Available methods are following

setZoom(zoom:number)

and

setCenter(latlng:LatLng)

Neither setCenter supports multiple location or Location array input nor setZoom does have this type of functionality

enter image description here

like image 663
Trikaldarshiii Avatar asked Oct 10 '13 19:10

Trikaldarshiii


People also ask

How do I set my map to zoom?

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 you calculate zoom level on a map?

Convert latitude, longitude to spherical mercator x, y. Get distance between your two points in spherical mercator. The equator is about 40m meters long projected and tiles are 256 pixels wide, so the pixel length of that map at a given zoom level is about 256 * distance/40000000 * 2^zoom.

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.


1 Answers

You need to use the fitBounds() method.

var markers = [];//some array var bounds = new google.maps.LatLngBounds(); for (var i = 0; i < markers.length; i++) {  bounds.extend(markers[i]); }  map.fitBounds(bounds); 

Documentation from developers.google.com/maps/documentation/javascript:

fitBounds(bounds[, padding])

Parameters:

`bounds`:  [`LatLngBounds`][1]|[`LatLngBoundsLiteral`][1] `padding` (optional):  number|[`Padding`][1] 

Return Value: None

Sets the viewport to contain the given bounds.
Note: When the map is set to display: none, the fitBounds function reads the map's size as 0x0, and therefore does not do anything. To change the viewport while the map is hidden, set the map to visibility: hidden, thereby ensuring the map div has an actual size.

like image 194
Adam Avatar answered Sep 18 '22 09:09

Adam