Created
July 14, 2013 00:27
-
-
Save ChrisLTD/5992716 to your computer and use it in GitHub Desktop.
Google Maps API V3 map w/ multiple markers, info boxes and auto center
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> | |
<title>Google Maps Multiple Markers</title> | |
<script src="http://maps.google.com/maps/api/js?sensor=false"></script> | |
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js"></script> | |
</head> | |
<body> | |
<div id="map" style="width: 500px; height: 400px;"></div> | |
<script type="text/javascript"> | |
var locations = [ | |
['Bondi Beach', -33.890542, 151.274856, 4], | |
['Coogee Beach', -33.923036, 151.259052, 5], | |
['Cronulla Beach', -34.028249, 151.157507, 3], | |
['Manly Beach', -33.80010128657071, 151.28747820854187, 2], | |
['Maroubra Beach', -33.950198, 151.259302, 1] | |
]; | |
var map = new google.maps.Map(document.getElementById('map'), { | |
zoom: 10, | |
center: new google.maps.LatLng(-39.92, 151.25), | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}); | |
var infowindow = new google.maps.InfoWindow(); | |
var marker, i; | |
var markers = new Array(); | |
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 | |
}); | |
markers.push(marker); | |
google.maps.event.addListener(marker, 'click', (function(marker, i) { | |
return function() { | |
infowindow.setContent(locations[i][0]); | |
infowindow.open(map, marker); | |
} | |
})(marker, i)); | |
} | |
function AutoCenter() { | |
// Create a new viewpoint bound | |
var bounds = new google.maps.LatLngBounds(); | |
// Go through each... | |
$.each(markers, function (index, marker) { | |
bounds.extend(marker.position); | |
}); | |
// Fit these bounds to the map | |
map.fitBounds(bounds); | |
} | |
AutoCenter(); | |
</script> | |
</body> | |
</html> |
is this code still relevant in 2020?
I am having a hard time figuring out a auto zoom on multiple markers map.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it work for me, but I want to add a blue marker on the current position, so with those below I can location my current position:
function success(position)
{
var googleLatLng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var mapOtn = {
zoom : 15,
center : googleLatLng,
maTyptId : google.maps.MapTypeId.ROADMAP
}
}
function addMarker(map, googleLatLng, title, content){
var markerOptn = {
position : googleLatLng,
map : map,
title : title,
animation : google.maps.Animation.BOUNCE
}
/Set multiple markers on my map/
/Set multiple markers on my map/
function fail(error)
{
var errorType = {
0: "Unknown Error",
1: "Permission denied by the user",
2: "Position of the user not available",
3: "Request timed out"
};
}
Please any help!