I’m currently using this code to create a map with three markers:
function initMap() {
var places = [
['PLACE 1', 41.926979, 12.517385],
['PLACE 2', 41.914873, 12.506486],
['PLACE 3', 41.918574, 12.507201]
];
var myMap = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 15,
center: new google.maps.LatLng(41.923, 12.513),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infoWindow = new google.maps.InfoWindow();
for (let j = 0; j < places.length; j++) {
let position = new google.maps.LatLng(places[j][1], places[j][2]);
let marker = new google.maps.Marker({
position: position,
map: myMap
});
google.maps.event.addListener(marker, 'click', (function(marker, j) {
return function() {
infoWindow.setContent(places[j][0]);
infoWindow.open(myMap, marker);
}
})(marker, j));
}
}
function loadGoogleMaps() {
var script = document.createElement('script');
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&callback=initMap';
document.body.appendChild(script);
}
window.onload = loadGoogleMaps;
My goal is to eliminate the need to manually set the map’s center using the coordinates center: new google.maps.LatLng(41.923, 12.513)
. Is there an automatic method to center the map based on the three marker locations?