Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

316 Part IV — Google Earth Hacks


my $sth = $dbh->prepare(sprintf(‘select * from ch10 order by type’));

$sth->execute();

my ($currenttype,$count) = (‘’,0);

while (my $row = $sth->fetchrow_hashref())
{
if ($currenttype ne $row->{type})
{
if ($count > 0)
{
print “</Folder>”;
}
printf(“<Folder>\n<name>%s</name>\n<open>0</open>\n”,ucfirst($row-> ;
{type}));
$currenttype = $row->{type};
}

$count++;

printf(“<Placemark>\n<name>%s</name>\n<address>%s,%s</address>\n<Point> ;
\n<coordinates>%s,%s,0</coordinates>\n</Point></Placemark>\n”,
$row->{title},
$row->{adda},
$row->{addb},
$row->{lng},
$row->{lat},
);

}
$sth->finish();

print(“</Folder></Folder>\n</kml>\n”);

Although not demonstrated in this script, error handling with dynamic KML files is very
strict, because Google Earth refuses to use the network link if the file is not valid. Therefore, all
dynamic KML generators should always return an HTTP response of 200. To show an error,
generate the appropriate KML, for example:
<?xml version=”1.0” encoding=”UTF-8”?>
<kml xmlns=”http://earth.google.com/kml/2.0”>
<Folder>
<name>Error loading file</name>
<open>1</open>
</Folder>
</kml>

Care should be taken to ensure you follow this approach.
Free download pdf