Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Creating a new map
- var map = new google.maps.Map(document.getElementById("map"), {
- mapTypeId: google.maps.MapTypeId.ROADMAP
- });
- // Creating a global infoWindow object that will be reused by all markers
- var infoWindow = new google.maps.InfoWindow();
- var bounds = new google.maps.LatLngBounds ();
- // Looping through the JSON data
- for (var i = 0, length = json.length; i < length; i++) {
- //console.log(data);
- var data = json[i],
- latLng = new google.maps.LatLng(data.companyLat, data.companyLon);
- // Creating a marker and putting it on the map
- if(latLng != '(0, 0)'){
- var marker = new google.maps.Marker({
- position: latLng,
- map: map,
- title: data.title
- });
- bounds.extend (latLng);
- // 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('<h5>' + data.title + '</h5>'/* + data.address + '<br>' + data.city + ', ' + data.state + ' ' + data.zipcode + '<br>' + data.phone + '<br>' + data.website*/);
- infoWindow.open(map, marker);
- });
- })(marker, data);
- }
- }
- map.fitBounds (bounds);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement