This is what I use to display a map with 3 pins/markers:
<script>
function initialize() {
var locations = [
['DESCRIPTION', 41.926979, 12.517385, 3],
['DESCRIPTION', 41.914873, 12.506486, 2],
['DESCRIPTION', 41.918574, 12.507201, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: new google.maps.LatLng(41.923, 12.513),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&' + 'callback=initialize';
document.body.appendChild(script);
}
window.onload = loadScript;
</script>
<div id="map" style="width: 900px; height: 700px;"></div>
What I’m looking for is a way to avoid having to “manually” find the center of the map with center: new google.maps.LatLng(41.923, 12.513)
. Is there a way to automatically have the map centered on the three coordinates?
To find the exact center of the map you'll need to translate the lat/lon coordinates into pixel coordinates and then find the pixel center and convert that back into lat/lon coordinates.
You might not notice or mind the drift depending how far north or south of the equator you are. You can see the drift by doing map.setCenter(map.getBounds().getCenter()) inside of a setInterval, the drift will slowly disappear as it approaches the equator.
You can use the following to translate between lat/lon and pixel coordinates. The pixel coordinates are based on a plane of the entire world fully zoomed in, but you can then find the center of that and switch it back into lat/lon.
I use the method above to set the map boundaries, then, instead of resetting the zoom level, I just calculate the average LAT and average LON and set the center point to that location. I add up all the lat values into latTotal and all the lon values into lontotal and then divide by the number of markers. I then set the map center point to those average values.
latCenter = latTotal / markercount; lonCenter = lontotal / markercount;
i had a situation where i can't change old code, so added this javascript function to calculate center point and zoom level:
Here's my take on this in case anyone comes across this thread:
This helps protect against non-numerical data destroying either of your final variables that determine
lat
andlng
.It works by taking in all of your coordinates, parsing them into separate
lat
andlng
elements of an array, then determining the average of each. That average should be the center (and has proven true in my test cases.)There's an easier way, by extending an empty
LatLngBounds
rather than creating one explicitly from two points. (See this question for more details)Should look something like this, added to your code:
This way, you can use an arbitrary number of points, and don't need to know the order beforehand.
Demo jsFiddle here: http://jsfiddle.net/x5R63/
I think you have to calculate latitudine min and longitude min: Here is an Example with the function to use to center your point: