Summary
This mini-project came about because I had a need to give people route instructions for a party.
It's easy to use
Google Maps∞ to set up a customised route by adding placemarks at navigationally-significant locations (see
this example∞), and you can easily add instructions (including pictures) at each point (try clicking on one of the "map pins").
However in the real world, there is nothing to beat having a list of instructions that each person can print out. I didn't want to re-type everything into a separate document, so created a
web page∞ to extract the key information from Google Maps and present it as a
tabular list∞. Editing the data in Google Maps is immediately reflected in the web page.
There are three key components which are described in more detail later in this page.
- The KML∞ representation of the map
- A PHP script to pull down the relevant KML file, manipulate it by application of an XSL stylesheet, and create the resulting web page
- The XSL stylesheet
Assumptions and Limitations
- Assumes that the order in which waypoints are added to the Google Map is the correct navigational order. (this could be a real pain if you later needed to add an intermediate point)
- URL transform to get KML file is hard-coded
- Assumes that the only placemarks on the map are navigational waypoints
References
Blog References
Details of Components
KML File
Google Maps conveniently publishes the relevant KML file at a URL which is the same as the map URL with
&output=kml appended.
Web Page
<?php
/*
Copyright Julian Elve 2007
Released under Creative Commons Attribution-NonCommercial-ShareAlike licence (http://creativecommons.org/licenses/by-nc-sa/2.5/deed.en_GB)
*/
$thispageurl =
"http://".
$_SERVER["HTTP_HOST"].
$_SERVER['PHP_SELF'];
$mapurl =
"http://maps.google.co.uk/maps/ms?ie=UTF8&hl=en&z=15&ll=51.511627,-0.096087&spn=0.011137,0.040169&t=h&om=1&msid=114870744536353390965.00000111ea7e65a3111ef&msa=0";
?>
<html>
<head>
<title>From Saint Paul
's to Tate Modern</title>
<style type="text/css" media="screen">
@import url( http://www.synesthesia.co.uk/data/route.css );
</style>
<link rel="stylesheet" type="text/css" media="print" href="http://www.synesthesia.co.uk/data/route-print.css" />
</head>
<body>
<div id = "wrapper">
<div id="header"><p>From Saint Paul's to Tate Modern</p><p>Walking Route To The Gallery</p></div>
<div
class=
"intro">
<div
class=
"source">
<p> The most up-to-
date version of this route sheet can be found at <a href=
"<?php echo $thispageurl; ?>"><?php
echo $thispageurl; ?></a></p>
<p>An online map of this route can be seen <a href=
"<?php echo $mapurl; ?>">here</a></p>
</div>
</div>
<?php printdirections
($mapurl .
"&output=kml");?>
</div> <!-- /wrapper -->
</body>
</html>
<?php
function printdirections
($kmlurl){
$xml =
'imported.xml';
$xsl =
'route.xsl';
if (!
$file =
fopen($kmlurl,
"r")) {
print "Cannot open remote URL ($kmlurl)";
exit;
}
while(!
feof($file))
$temp .=
fgets($file,
1024);
fclose($file);
if (!
$file =
fopen($xml,
"w")){
print "Cannot open file ($xml)";
exit;
}
if (fwrite($file,
$temp) ===
FALSE) {
print "Cannot write to file ($xml)";
exit;
}
fclose($file);
// Allocate a new XSLT processor
$xh = xslt_create
();
// Process the document, returning the result into the $result variable
$result = xslt_process
($xh,
$xml,
$xsl);
if ($result) {
//print "SUCCESS, sample.xml was transformed by sample.xsl into the \$result";
//print " variable, the \$result variable has the following contents\n<br>\n";
//print "<pre>\n";
//print $result;
print html_entity_decode($result);
//print "</pre>\n";
}
else {
print "Sorry, could transform the source";
print " the reason is that " . xslt_error
($xh) .
print " and the error code is " . xslt_errno
($xh);
}
xslt_free
($xh);
}
?>
XSL Stylesheet
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:kml="http://earth.google.com/kml/2.0">
<xsl:output method="html" version="4.0"
encoding="iso-8859-1"
omit-xml-declaration="yes"/>
<!-- this version of the stylesheet assumes that the output
will be embedded in a page that handles html head and body elements
-->
<xsl:template match="/">
<div class = "waypoint-list">
<!--we want all the placemarks that are not the route line-->
<xsl:apply-templates select="kml:kml/kml:Document/kml:Placemark[not(kml:LineString)]"/>
</div>
</xsl:template>
<xsl:template match="kml:Placemark">
<div class="waypoint">
<table>
<tr>
<td class="instruction">
<!--<p>
<xsl:value-of select="namespace-uri()" /></p>-->
<!--<p>
<xsl:value-of select="name()" /></p>-->
<p><b><xsl:value-of select="*[local-name()='name']"/></b></p></td>
<td class="description">
<xsl:value-of select="*[local-name()='description']" />
</td>
</tr>
</table>
</div>
</xsl:template>
</xsl:stylesheet>
Cite as "Multiple Authors; Summary
. Synesthesia Wiki; Retrieved 5th Dec 2008 from http://www.synesthesia.co.uk/wikka/ExtractingRouteInstructionsFromGoogleMap"