Geo-Google
view release on metacpan or search on metacpan
lib/Geo/Google/Path.pm view on Meta::CPAN
my $self = bless \%arg, $class;
}
sub distance { return shift->{'distance'} }
sub polyline { return shift->{'polyline'} }
sub segments { my $self = shift; return $self->{'segments'} ? @{ $self->{'segments'} } : () }
sub time { return shift->{'time'} }
sub locations { my $self = shift; return $self->{'locations'} ? @{ $self->{'locations'} } : () }
sub panel { return shift->{'panel'} }
sub levels { return shift->{'levels'} }
sub toXML {
my $self = shift;
my $content = join "", map { $_->toXML } @{ $self->{segments} };
return sprintf( FMT,
$self->distance(),
$self->time(),
$content,
);
}
sub toJSON {
my $self = shift;
my $json = new JSON (barekey => 1);
# Construct the shell of a new perl data structure that we
# can pass off to the JSON module for rendering to a string
my $preJSON = Geo::Google::_JSONrenderSkeleton();
# Fill the perl data structure with information from this
# location
my @locations = $self->locations();
$preJSON->{"form"}->{"l"}->{"near"} =
$locations[$#locations]->title();
$preJSON->{"form"}->{"q"}->{"q"} = "from:" .
$locations[0]->title();
for (my $i=1; $i<=$#locations; $i++) {
$preJSON->{"form"}->{"q"}->{"q"} .= " to:" .
$locations[$i]->title();
}
$preJSON->{"form"}->{"d"}->{"saddr"} = $locations[0]->title();
$preJSON->{"form"}->{"d"}->{"daddr"} = $locations[1]->title();
for (my $i=2; $i<=$#locations; $i++) {
$preJSON->{"form"}->{"d"}->{"daddr"} .= " to:" .
$locations[$i]->title();
}
$preJSON->{"form"}->{"selected"} = "q";
$preJSON->{"panelStyle"} = "";
# Generate the markers
for (my $i=0; $i<=$#locations; $i++) {
my $image = "/mapfiles/dd-pause.png";
my $id = "pause";
if ($i == 0 ) {
$image = "/mapfiles/dd-start.png";
$id = "start";
}
elsif ($i == $#locations) {
$image = "/mapfiles/dd-stop.png";
$id = "stop";
}
push( @{$preJSON->{"overlays"}->{"markers"}}, {
"laddr" => "",
"svaddr" => "",
"lat" => $locations[$i]->latitude(),
"lng" => $locations[$i]->longitude(),
"id" => $id,
"image" => $image,
"elms" => [()],
"llcid" => "",
"fid" => "",
"cid" => "",
"sig" => "",
"infoWindow" => {
"type" => "map",
"_" => 0
}
});
}
$preJSON->{"overlays"}->{"polylines"}->[0]->{"levels"} =
$self->levels();
$preJSON->{"overlays"}->{"polylines"}->[0]->{"points"} =
$self->polyline();
$preJSON->{"overlays"}->{"polylines"}->[0]->{"outline"} = undef;
$preJSON->{"overlays"}->{"polylines"}->[0]->{"opacity"} = undef;
$preJSON->{"overlays"}->{"polylines"}->[0]->{"color"} = undef;
$preJSON->{"overlays"}->{"polylines"}->[0]->{"weight"} = undef;
$preJSON->{"overlays"}->{"polylines"}->[0]->{"id"} = "d";
$preJSON->{"overlays"}->{"polylines"}->[0]->{"numLevels"} = 4;
$preJSON->{"overlays"}->{"polylines"}->[0]->{"zoomFactor"} = 16;
$preJSON->{"printheader"} = sprintf("Directions from %s to %s"
. "<BR />Travel time: %s",
$locations[0]->title(),
$locations[$#locations]->title(),
$self->time());
$preJSON->{"modules"} = [(undef, "multiroute")];
$preJSON->{"panelResizeState"} = "open";
# step through all points in the directions and determine
# the maximum and minimum lat/longs
my $maxlat = -90;
my $minlat = 90;
my $maxlng = -180;
my $minlng = 180;
foreach my $segment ($self->segments()) {
foreach my $point ($segment->points()) {
if ($point->latitude() > $maxlat)
{ $maxlat = $point->latitude(); }
if ($point->latitude() < $minlat)
{ $minlat = $point->latitude(); }
if ($point->longitude() > $maxlng)
{ $maxlng = $point->longitude(); }
if ($point->longitude() < $minlng)
{ $minlng = $point->longitude(); }
}
}
$preJSON->{"viewport"}->{"span"}->{"lat"} = sprintf("%.6f",
( ($maxlat-$minlat) * 1.1) );
$preJSON->{"viewport"}->{"span"}->{"lng"} = sprintf("%.6f",
( ($maxlng-$minlng) * 1.1) );
$preJSON->{"viewport"}->{"center"}->{"lat"} = sprintf("%.6f",
( ($maxlat+$minlat) / 2) );
$preJSON->{"viewport"}->{"center"}->{"lng"} = sprintf("%.6f",
( ($maxlng+$minlng) / 2) );
$preJSON->{"viewport"}->{"mapType"} = undef;
$preJSON->{"url"} = "/maps?v=1&q=" .
uri_escape(sprintf("from:%s to:%s",
$locations[0]->title(),
$preJSON->{"form"}->{"d"}->{"daddr"}))
. "&ie=UTF8";
$preJSON->{"title"} = sprintf("From:%s to:%s",
$locations[0]->title(),
$preJSON->{"form"}->{"d"}->{"daddr"});
$preJSON->{"panel"} = $self->panel();
# Render the data structure to a JSON string and return it
return $json->objToJson($preJSON);
}
1;
__END__
Below is stub documentation for your module. You'd better edit it!
=head1 NAME
Geo::Google::Path - A path, by automobile, between two loci.
=head1 SYNOPSIS
use Geo::Google::Path;
# you shouldn't need to construct these yourself,
( run in 1.263 second using v1.01-cache-2.11-cpan-39bf76dae61 )