Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

Chapter 16: Generating Google Earth Feeds


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


$sth->execute();


To generate the individual tags, you only have to generate a suitable XML
structure. The $currenttypevariable is used to identify first the current type so that you can
determine whether to create a new folder. The $countvariable is used to identify the count to
determine whether the closing tag needs to be created (for the first change of type, it doesn’t).
Note that for the subfolders, you don’t open the contents; this doesn’t affect whether the
enclosed points are displayed, only whether the list of points in the folder is displayed:


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


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


$count++;

Each individual placemark is composed of the name, coordinates, and address of each entity in
the database:


printf(“\n%s\n

%s,%s
;
\n \n%s,%s,0 ;
\n</\Point>\n”,
$row->{title},
$row->{adda},
$row->{addb},
$row->{lng},
$row->{lat},
);


}
$sth->finish();


Finally, you close the last entity type folder, the global KML folder, and finally the KML docu-
ment itself:


print(“\n\n”);


Using the information that is in the final version of the database created in Chapter 11, the
preceding script generates the following KML. The quantity of KML included here is shown
to provide an overview of the overall structure, as well as the individual points. Even so, the
document has been shortened for inclusion in the book.

Free download pdf