Geo-Google-PolylineEncoder
view release on metacpan or search on metacpan
The module is a port of Mark McClure's `PolylineEncoder.js' with some
tweaks. The original can be found here:
http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/
CONSTRUCTOR & ACCESSORS
new( [%args] )
Create a new encoder. Arguments are optional and correspond to the
accessor with the same name: the num_levels, zoom_factor,
visible_threshold, and force_endpoints entries elsewhere in this
document, etc...
Note: there's nothing stopping you from setting these properties
each time you encode a polyline.
num_levels
How many different levels of magnification the polyline has.
Default: 18.
zoom_factor
The change in magnification between those levels (see num_levels).
Default: 2.
visible_threshold
Indicates the length of a barely visible object at the highest zoom
level. Default: 0.00001. err.. units.
force_endpoints
Indicates whether or not the endpoints should be visible at all zoom
levels. force_endpoints is. Probably should stay true regardless.
Default: 1=true.
escape_encoded_points
Indicates whether or not the encoded points should have escape
characters escaped, eg:
$points =~ s/\\/\\\\/g;
This is useful if you'll be evalling the resulting strings, or
copying them into a static document.
lib/Geo/Google/PolylineEncoder.pm view on Meta::CPAN
};
var line = GPolyline.fromEncoded( opts );
=cut
package Geo::Google::PolylineEncoder;
use strict;
use warnings;
use accessors qw(num_levels zoom_factor visible_threshold force_endpoints
zoom_level_breaks escape_encoded_points lons_first
points dists max_dist encoded_points encoded_levels );
use constant defaults => {
num_levels => 18,
zoom_factor => 2,
force_endpoints => 1,
escape_encoded_points => 0,
visible_threshold => 0.00001,
lons_first => 0,
};
our $VERSION = 0.06;
# The constructor
sub new {
my $class = shift;
my $self = bless {}, $class;
lib/Geo/Google/PolylineEncoder.pm view on Meta::CPAN
my $max_dist = $self->max_dist;
# Cache for performance:
my $num_levels = $self->num_levels;
my $num_levels_minus_1 = $num_levels - 1;
my $visible_threshold = $self->visible_threshold;
my $zoom_level_breaks = $self->zoom_level_breaks;
my $encoded_levels = "";
if ($self->force_endpoints) {
$encoded_levels .= $self->encode_number($num_levels_minus_1);
} else {
$encoded_levels .= $self->encode_number($num_levels_minus_1 - $self->compute_level($max_dist));
}
# Note: skip the first & last point:
for my $i (1 .. scalar(@$points) - 2) {
my $dist = $dists->[$i];
if (defined $dist) {
lib/Geo/Google/PolylineEncoder.pm view on Meta::CPAN
if ($dist > $visible_threshold) {
while ($dist < $zoom_level_breaks->[$level]) {
$level++;
}
}
$encoded_levels .= $self->encode_number($num_levels_minus_1 - $level);
}
}
if ($self->force_endpoints) {
$encoded_levels .= $self->encode_number($num_levels_minus_1);
} else {
$encoded_levels .= $self->encode_number($num_levels_minus_1 - $self->compute_level($max_dist));
}
$self->encoded_levels( $encoded_levels );
}
# This computes the appropriate zoom level of a point in terms of it's
lib/Geo/Google/PolylineEncoder.pm view on Meta::CPAN
L<http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/>
=head1 CONSTRUCTOR & ACCESSORS
=over 4
=item new( [%args] )
Create a new encoder. Arguments are optional and correspond to the accessor
with the same name: L</num_levels>, L</zoom_factor>, L</visible_threshold>,
L</force_endpoints>, etc...
Note: there's nothing stopping you from setting these properties each time you
L</encode> a polyline.
=item num_levels
How many different levels of magnification the polyline has.
Default: 18.
=item zoom_factor
The change in magnification between those levels (see L</num_levels>).
Default: 2.
=item visible_threshold
Indicates the length of a barely visible object at the highest zoom level.
Default: 0.00001. err.. units.
=item force_endpoints
Indicates whether or not the endpoints should be visible at all zoom levels.
force_endpoints is. Probably should stay true regardless.
Default: 1=true.
=item escape_encoded_points
Indicates whether or not the encoded points should have escape characters
escaped, eg:
$points =~ s/\\/\\\\/g;
This is useful if you'll be evalling the resulting strings, or copying them into
( run in 1.640 second using v1.01-cache-2.11-cpan-2b1a40005be )