2amigos / yii2-google-maps-library

Google Maps API library for Yii2
https://2amigos.us
BSD 3-Clause "New" or "Revised" License
106 stars 78 forks source link

Zoom and show all the markers #64

Open ghost opened 7 years ago

ghost commented 7 years ago

Option to create a LatLngBounds to be able to zoom automatically to show all markers in map?

tonydspaniard commented 7 years ago

First get the bounds of all your markers (https://github.com/2amigos/yii2-google-maps-library/blob/master/LatLngBounds.php#L357), then get the center of the coordinates to set center your map (https://github.com/2amigos/yii2-google-maps-library/blob/master/LatLngBounds.php#L124) and also your zoom (https://github.com/2amigos/yii2-google-maps-library/blob/master/LatLngBounds.php#L189) you can later initialize the map and will show all your markers.

jlnarvaez commented 6 years ago

@tonydspaniard solution with code:

$marker1 = new Marker([
    'position' =>  new LatLng(['lat' => 0.53, 'lng' => 2.47]),
    'title' => 'Test',
]);

$marker2 = new Marker([
    'position' => new LatLng(['lat' => 0.88, 'lng' => 5.32]),
    'title' => 'Test',
]);

First get the bounds of all your markers: $latlng = LatLngBounds::getBoundsOfMarkers([$marker1, $marker2]);

Then get the center of the coordinates to set center your map: $center = $latlng->getCenterCoordinates();

And also your zoom: $zoom = $latlng->getZoom(300);

$map = new Map([
    'center' => $center,
    'zoom' => $zoom,
]);

// Here add markers to map and display map