Open ghost opened 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.
@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
Option to create a LatLngBounds to be able to zoom automatically to show all markers in map?