Skip to content

Instantly share code, notes, and snippets.

@saifazmi
Last active March 23, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saifazmi/be7ac94498e5b9cf8874c241e2211ab6 to your computer and use it in GitHub Desktop.
Save saifazmi/be7ac94498e5b9cf8874c241e2211ab6 to your computer and use it in GitHub Desktop.
Snippet: JS function to intialise a GMap with a marker and infoWindow
function initMap() {
// Office lat lang
var officeLatLang = {
lat: -25.363,
lng: 131.044
};
// infoWindow content string
var infoContent = '<p>55 Dangal Road,<br>' +
'Vikas Nagar, Lucknow<br>' +
'Uttarpradesh, 226022<br>' +
'India</p>';
// Initialise the infoWindow with the content string
var infowindow = new google.maps.InfoWindow({
content: infoContent
});
// Initialise the map with required options
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4, // map zoom level
center: officeLatLang, // coordinates to center on
scrollwheel: false // prevent accidental scrolling of map
});
// Initialise the marker and add to map
var marker = new google.maps.Marker({
position: officeLatLang, // marker coordinates
map: map // map to place on
});
// Display infoWindow on marker click
marker.addListener('click', function() {
infowindow.open(map, marker);
});
// Display infoWindow onLoad
infowindow.open(map, marker);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment