Skip to content

Instantly share code, notes, and snippets.

@raazon
Created December 3, 2019 07:47
Show Gist options
  • Save raazon/68e451b425ac3b880d73be45f800b3f5 to your computer and use it in GitHub Desktop.
Save raazon/68e451b425ac3b880d73be45f800b3f5 to your computer and use it in GitHub Desktop.
Google map multiple marker with auto center and zoom
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Multiple Google Map Marker</title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key="></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="mapmarker.jquery.js"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = [
["11+ acres of Prime Commercial Land in Kettleman City", "35.991996", "-119.961455", 1],
["NNN Cardiologist Medical Office", "33.18079129999999", "-97.0921624", 2],
["NYC Townhouse with Income", "40.7493228", "-73.97824349999996", 3],
["Walgreens, Camp Verde, AZ", "34.5654008", "-111.86216839999997", 4],
["San Francisco Redevelopment Opportunity", "37.7195681", "-122.43834129999999", 5],
["Cal Twin Towers", "35.3653436", "-119.05626610000002", 6],
["Las Vegas Corp. Guaranteed NNN SNTL Building (NASDAQ:GDEN)", "36.08632300000001", "-115.3148261", 7],
["Las Vegas Corp. Guaranteed NNN SNTL Building (NASDAQ:GDEN)", "36.0680489", "-115.2974175", 8],
["Mixed-Use Building", "45.515331", "-123.876327", 9],
["42056 SF Industrial Building", "39.7955703", "-104.96874459999998", 10],
["1050 Miller Drive", "28.6807826", "-81.35154310000001", 11],
["KEYBANK", "40.31542178214287", "-79.68340002000332", 12]
]
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(23.8697847, 90.4219536),
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...
jQuery.each(markers, function (index, marker) {
bounds.extend(marker.position);
});
// Fit these bounds to the map
map.fitBounds(bounds);
}
AutoCenter();
</script>
</body>
</html>
<!-- Credit: https://github.com/aslamdoctor/jQuery.mapmarker -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment