Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

256 Part III — Google Map Hacks


Nothing else changes aside from the status message. By not zeroing the list of points or chang-
ing any other details, the application returns the route and retains the route display and,
because the enable process doesn’t modify anything either, the recording process can be
restarted.

Clearing the Last Point


So that users can edit an existing route (or a new route that they are recoding), the application
includes the ability to remove the last point added to a route. This is achieved by popping the
last point off of the array (which removes the point from the array). Then the existing overlay is
removed, the GPolylineobject is re-created, and the overlay is added back:
function clearLastPoint() {
if (points.length > 0) {
points.pop();
}
if (points.length == 0) {
map.clearOverlays();
return;
}
map.removeOverlay(route);
route = new GPolyline(points);
map.addOverlay(route);
}

The application only pops a point off the array if the array has at least one item. If all of the
points in the array are removed from the display, the starting marker object is also removed.

Clearing the Current Route


If the user wants to clear the current route but not delete the route from the list of saved routes,
a convenient function is provided that clears all the overlays (including the starting marker and
any GPolylineoverlays) and then empties the array of existing points:
function clearRoute() {
map.clearOverlays();
points = [];
}

Note that nothing else has changed. The application could still be in recording mode, and the
process would continue as normal.

Initializing a New Route.


Different from clearing the route, creating a new route zeros not only the list of points, but also
all the other variables, before enabling the recording of the route by calling startRoute():
function newRoute() {
points = [];
routeidfield.value = 0;
routetitle.value = ‘’;
Free download pdf