Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

220 Part III — Google Map Hacks


Now, for each marker the addmarker()function is called to create the marker with the cus-
tom icon:
for (var i=0;i < markerlist.length;i++) {
addmarker(parseFloat(markerlist[i].getAttribute(“lng”)),
parseFloat(markerlist[i].getAttribute(“lat”)),
markerlist[i].getAttribute(“title”),
infowindow[i],
baseIcon,
entitytype);
}

Once all the markers have been created, the map is recentered. However, the application no
longer builds an array of points to be used:
recenterandzoom(markers);
infopanel.innerHTML = infopanel.innerHTML +
‘<br/>’ +
‘<a href=”#” onClick=”showentitylist()”> ;
Back to business types</a><br/>’;
}
}
request.send(null);
}

Instead, you supply the array of markers that have been added, and a small modification is
made to the recenterandzoom()function to use the new object array for the source
information.

Recentering the Map


To recenter the map, you need to extract the data from the marker objects. To do this, you
make a small change to the function to extract the data straight from the objects, rather than
from the array of points from a global variable used in previous examples. The remainder of the
function is the same, though:
function recenterandzoom(markers) {
var latpoints = [];
var lngpoints = [];

for(var i=0;i<markers.length;i++) {
latpoints.push(markers[i].lat);
lngpoints.push(markers[i].lng);
}

latpoints.sort(function(x,y) { return x-y; });
lngpoints.sort(function(x,y) { return x-y; });
var newlat = latpoints[0] + ((latpoints[latpoints.length-1] - ;
latpoints[0])/2);
Free download pdf