27.10.2014 Views

Google Maps API 3

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9 ■ DEALING WITH MASSIVE NUMBERS OF MARKERS<br />

The Final Code for This Example<br />

Listing 9-7 shows the final JavaScript code for this example.<br />

Listing 9-7. The Final JavaScript Code for Example 9-3<br />

(function() {<br />

window.onload = function() {<br />

// Creating a map<br />

var options = {<br />

zoom: 5,<br />

center: new google.maps.LatLng(37.99, -93.77),<br />

mapTypeId: google.maps.MapTypeId.ROADMAP<br />

};<br />

var map = new google.maps.Map(document.getElementById('map'), options);<br />

// Creating a new MarkerManager object<br />

var mgr = new MarkerManager(map);<br />

// Creating an array that will contain all of the markers<br />

var markers = [];<br />

// Setting the boundaries within where the markers will be created<br />

var southWest = new google.maps.LatLng(24, -126);<br />

var northEast = new google.maps.LatLng(50, -60);<br />

var lngSpan = northEast.lng() - southWest.lng();<br />

var latSpan = northEast.lat() - southWest.lat();<br />

// Creating markers at random locations<br />

for (var i = 0; i < 100; i++) {<br />

}<br />

// Calculating a random location<br />

var lat = southWest.lat() + latSpan * Math.random();<br />

var lng = southWest.lng() + lngSpan * Math.random();<br />

var latlng = new google.maps.LatLng(lat, lng);<br />

// Creating a marker<br />

var marker = new google.maps.Marker({<br />

position: latlng<br />

});<br />

// Adding the marker to the array<br />

markers.push(marker);<br />

// Making sure the MarkerManager is properly loaded before we use it<br />

google.maps.event.addListener(mgr, 'loaded', function() {<br />

// Adding the markers to the MarkerManager<br />

mgr.addMarkers(markers, 1);<br />

// Adding the markers to the map<br />

200

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!