Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

288 Part III — Google Map Hacks


Using JavaScript you can determine the location of a string within a string using the
indexOf()method on the stringobject. Because you are looking for a prefix of geo:lon,
the return value should be zero. If it is, you split the string on the =character, the right hand of
which should be a floating-point value for the longitude. The same process can be repeated for
the latitude.
if (tags[i].getAttribute(‘raw’).indexOf(‘geo:lon’) == 0) {
var elems = tags[i].getAttribute(‘raw’).split(‘=’);
lng = elems[1];
}
if (tags[i].getAttribute(‘raw’).indexOf(‘geo:lat’) == 0) {
var elems = tags[i].getAttribute(‘raw’).split(‘=’);
lat = elems[1];
}

If you have both the latitude and longitude, you can then create a point and a marker. You can
point to the XSL Transformation and the original XML returned by the HTTP request to
generate a suitable info window:
if (lat && lng) {
var point = new GPoint(parseFloat(lng),parseFloat(lat));
var marker = new GMarker(point);
map.addOverlay(marker);
GEvent.addListener(marker,
‘click’,
function() {
marker.openInfoWindowXslt ;
(xmlsource,”/examples/ch14-02.xsl”);
}
);
geopoints.push(point);
}
}

Finally, you request the photo from the list of available photos for the same process. If this was
the last request in the list, then recenter and zoom (using the function used in previous exam-
ples) to re-align the map on the selected photos:
index++;
if (index < photoids.length) {
getphotoinfo(index);
} else {
recenterandzoom(geopoints);
}
}
}

request.send(null);
}

You can see the basic result and the information window generated when the user clicks a
marker in Figures 14-1 and 14-2, respectively.
Free download pdf