Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

128 Part II — Instant Gratification


Clicking either marker will bring up an info window. However, the listener for opening the
window is displayed only in the latter map.

Monitoring Location


The last script in this quick overview of extending the basic examples given by Google Maps
demonstrates what is probably the most useful map in the map developer’s arsenal.

One of the problems with developing maps is that, as you build data and add markers, poly-
lines, and other information, you need to be able to find out your exact location on the map.
You must know this information in order add the latitude and longitude information to your
own applications. Listing 7-12 shows the code for this application.

Listing 7-12: Finding Your Location

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=UTF-8”/>

<title>MCslp Maps Chapter 7, Ex 12</title>
<script src=”http://maps.google.com/maps?file=api&v=1&key=XXX”
type=”text/javascript”>
</script>

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

var map;

function onLoad() {
if (GBrowserIsCompatible()) {
map = new GMap(document.getElementById(“map”));

GEvent.addListener(map, ‘click’, function(overlay,point) {
var latLngStr = ‘(‘ + point.x + ‘, ‘ + point.y + ‘)<br/>’;
var message = document.getElementById(“message”);
message.innerHTML = latLngStr;
});

map.centerAndZoom(new GPoint(-0.64,52.909444), 2);
}
}

//]]>
Free download pdf