Advertisement
developerjustin

Untitled

Feb 27th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.        
  2.         // Creating a new map
  3.         var map = new google.maps.Map(document.getElementById("map"), {
  4.           mapTypeId: google.maps.MapTypeId.ROADMAP
  5.         });
  6.  
  7.         // Creating a global infoWindow object that will be reused by all markers
  8.         var infoWindow = new google.maps.InfoWindow();
  9.         var bounds = new google.maps.LatLngBounds ();
  10.         // Looping through the JSON data
  11.         for (var i = 0, length = json.length; i < length; i++) {
  12.         //console.log(data);
  13.             var data = json[i],
  14.                 latLng = new google.maps.LatLng(data.companyLat, data.companyLon);
  15.             // Creating a marker and putting it on the map
  16.             if(latLng != '(0, 0)'){
  17.                 var marker = new google.maps.Marker({
  18.                     position: latLng,
  19.                     map: map,
  20.                     title: data.title
  21.                 });
  22.                 bounds.extend (latLng);
  23.                 // Creating a closure to retain the correct data, notice how I pass the current data in the loop into the closure (marker, data)
  24.                 (function(marker, data) {
  25.  
  26.                     // Attaching a click event to the current marker
  27.                     google.maps.event.addListener(marker, "click", function(e) {
  28.                         infoWindow.setContent('<h5>' + data.title + '</h5>'/* + data.address + '<br>' + data.city + ', ' + data.state + ' ' + data.zipcode + '<br>' + data.phone + '<br>' + data.website*/);
  29.                         infoWindow.open(map, marker);
  30.                     });
  31.                 })(marker, data);
  32.             }
  33.         }
  34.        
  35.         map.fitBounds (bounds);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement