I am using this code. Hope this helps you:
(function() { window.onload = function() { // Creating a new map var map = new google.maps.Map(document.getElementById("map"), { center: new google.maps.LatLng(41.056466, -85.3312009), disableDefaultUI: false, zoom: 16, mapTypeId: google.maps.MapTypeId.SATELLITE }); // Creating the JSON data var json = [ { "title": "Title 1", "lat": 41.057814980291, "lng": -85.329851919709, "description": "" }, { "title": "Title 2", "lat": 41.057814981000, "lng": -85.8048, "description": "" }, ] var styles = [ { "featureType": "water", "elementType": "geometry.fill", "stylers": [ { "visibility": "on" }, { "color": "#0077bb" }, { "lightness": 70 } ] },{ "featureType": "landscape.natural", "elementType": "geometry.fill", "stylers": [ { "visibility": "on" }, { "saturation": -100 }, { "color": "#699e6b" }, { "lightness": 76 } ] },{ "featureType": "poi.park", "elementType": "geometry.fill", "stylers": [ { "visibility": "off" } ] },{ "featureType": "road.local", "elementType": "geometry.fill", "stylers": [ { "visibility": "on" }, { "color": "#ffffff" } ] } ]; map.setOptions({styles: styles}); // Creating a global infoWindow object that will be reused by all markers var infoWindow = new google.maps.InfoWindow(); // Looping through the JSON data for (var i = 0, length = json.length; i < length; i++) { var data = json[i], latLng = new google.maps.LatLng(data.lat, data.lng); // Creating a marker and putting it on the map var marker = new google.maps.Marker({ position: latLng, map: map, title: data.title }); // Creating a closure to retain the correct data, notice how I pass the current data in the loop into the closure (marker, data) (function(marker, data) { // Attaching a click event to the current marker google.maps.event.addListener(marker, "click", function(e) { infoWindow.setContent(data.description); infoWindow.open(map, marker); }); })(marker, data); } } })();
Joan Manuel Hernández
source share