Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

118 Part II — Instant Gratification


Adding a Route
Adding a route is much the same as adding a bounding box: You build an array into which you
place list of points and then get the GPolylinefunction to plot those points and add them as
an overlay to your map. Unlike with a bounding box or other shape, you don’t try to complete
the circuit.

Listing 7-8 shows an example of how to add a route to a map by hard-coding a series of points.
In this case, I’m demonstrating a route, by road, for getting from one of the larger parking lots
in Grantham to Siam Garden.

Listing 7-8:Adding a Route

<script type=”text/javascript”>
//<![CDATA[

var map;

function onLoad() {
if (GBrowserIsCompatible()) {
map = new GMap(document.getElementById(“map”));
map.centerAndZoom(new GPoint(-0.64,52.909444), 2);
addmarker(-0.6394,52.9114);
addmarker(-0.64,52.909444);
addmarker(-0.6376,52.9073);

var route = [];

route.push(new GPoint(-0.6467342376708984, 52.91519081031524));
route.push(new GPoint(-0.6466054916381836, 52.915527220441405));
route.push(new GPoint(-0.6440305709838867, 52.916122401186186));
route.push(new GPoint(-0.6434726715087891, 52.91578599568332));
route.push(new GPoint(-0.6442880630493164, 52.915294321401625));
route.push(new GPoint(-0.6411123275756836, 52.9104807941625));
route.push(new GPoint(-0.6398248672485352, 52.90952320075754));
route.push(new GPoint(-0.638279914855957, 52.90778912639283));
route.push(new GPoint(-0.6376361846923828, 52.90716794854011));
map.addOverlay(new GPolyline(route));
}
}

function addmarker(x,y) {
var point = new GPoint(parseFloat(x),parseFloat(y));
map.addOverlay(new GMarker(point));
}

//]]>
</script>
Free download pdf