Skip to content

Instantly share code, notes, and snippets.

@jsakhil
Created December 2, 2019 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsakhil/146baaeabc6ff4351d7aa39d45e5e261 to your computer and use it in GitHub Desktop.
Save jsakhil/146baaeabc6ff4351d7aa39d45e5e261 to your computer and use it in GitHub Desktop.
GOOGLE MAP WITH MULTIPLE MARKERS AND CUSTOM MARKER ICON
<!-- GOOGLE MAP -->
<script async="" defer="" type="text/javascript" src="https://maps.googleapis.com/maps/api/js?callback=initializeMap&key=<--KEY-->"></script>
<!-- GOOGLE MAP -->
<script type="text/javascript">
//INITIALIZE GOOGLE MAP WITH MULTIPLE MARKERS AND CUSTOM MARKER ICON.
var markers = [
['Bondi Beach', -33.890542, 151.274856],
['Coogee Beach', -33.923036, 151.259052],
['Cronulla Beach', -34.028249, 151.157507],
['Manly Beach', -33.80010128657071, 151.28747820854187],
['Maroubra Beach', -33.950198, 151.259302]
];
function initializeMap() {
var latlng = new google.maps.LatLng(-33.92, 151.25);
var myOptions = {
scrollwheel: false,
draggable: true,
disableDefaultUI: false,
center: latlng,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//BIND MAP TO ELEMET WITH ID MAP-CANVAS
var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
var infowindow = new google.maps.InfoWindow(), marker, i;
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < markers.length; i++) {
var pos = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(pos);
marker = new google.maps.Marker({
position: pos,
icon: location.href + '/images/map-marker.png',
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(markers[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
map.fitBounds(bounds);
}
}
</script>
<div class="map-wrapper">
<div class="map-canvas" id="map-canvas">Loading map...</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment