--- web/googlemap.cgi 2007/12/07 00:59:05 24 +++ web/googlemap.cgi 2007/12/07 02:38:47 25 @@ -45,18 +45,24 @@ ), $q->submit( -value => 'Show trace' ), $q->br, - 'Draw as ', - $q->checkbox( - -name => 'line', + 'Draw ', + $q->popup_menu( + -name => 'points_filter', + -values => [ 'all', 'every', 'total of' ], + ), + $q->textfield( + -name => 'points_count', + -value => '', + -size => 2, -onChange => 'trace_frm.submit()', ), - ' with ', + ' points and ', $q->popup_menu( -name => 'placemark_filter', - -values => [ 'every', 'total of' ], + -values => [ 'total of', 'every' ], ), $q->textfield( - -name => 'placemark', + -name => 'placemark_count', -value => 5, -size => 2, -onChange => 'trace_frm.submit()', @@ -72,7 +78,6 @@ $trace = "$trace_path/$trace"; - my $points = 0; my $center_point; my @points; my @placemarks; @@ -96,12 +101,10 @@ ), }; - $points++; - } close($fh); - if ( $points > 0 ) { + if ( $#points >= 0 ) { my $map = HTML::GoogleMaps->new( key => $map_key, @@ -116,47 +119,87 @@ $map->map_type('hybrid'); $map->center( $center_point ) if $q->param('line') && $center_point; - if ( $q->param('line') ) { - warn "## points = ",dump( @points ); - $map->add_polyline( points => [ @points ] ); - } - - my $placemarks = 0; - - if ( my $placemark = $q->param('placemark') ) { - if ( $q->param('placemark_filter') eq 'every' ) { - foreach my $o ( 0 .. $#placemarks ) { - next unless $o % $placemark == 0; - $map->add_marker( %{ $placemarks[$o] } ); - $placemarks++; + sub filter_array { + my $o = {@_}; + my ( $count, $filter, $code ) = + ( $o->{count}, $o->{filter}, $o->{code} ) ; + confess "no CODE?" unless ref($code) eq 'CODE'; + my @array = @{ $o->{array} }; + + warn "count: $count filter: $filter\n"; + + my $code_calls = 0; + + if ( $count && $filter =~ m/every/ ) { + foreach my $o ( 0 .. $#array ) { + next unless $o % $count == 0; + $code->( $array[$o] ); + $code_calls++; } - } else { + } elsif ( $count && $filter =~ m/total/ ) { # total of - my $d = $points / ( $placemark - 1 ); - foreach my $p ( 0 .. $placemark - 2 ) { + if ( $count < 2 ) { + # first + if ( $array[0]) { + $code->( $array[0] ); + $code_calls++; + }; + # last + if ( $count > 1 && $#array > 0 ) { + $code->( $array[$#array] ); + $code_calls++; + }; + return $code_calls; + } + + my $d = $#array / ( $count - 1 ); + foreach my $p ( 0 .. $count - 1 ) { my $o = int($p * $d); - die "no placemark $p at $o from total of $#placemarks" unless $placemarks[$o]; - $map->add_marker( %{ $placemarks[$o] } ); - $placemarks++; + die "no element $p at $o from total of ",$#array + 1 unless $array[$o]; + $code->( $array[$o] ); + $code_calls++; + } + } else { + # show every + foreach my $e ( @array ) { + $code->( $e ); + $code_calls++; } - # add last one - $map->add_marker( %{ $placemarks[$#placemarks] } ); - $placemarks++; - } - } else { - # show every placemark - foreach my $marker ( @placemarks ) { - $map->add_marker( %$marker ); - $placemarks++; } + return $code_calls; } + my @poly_points; + my $points = filter_array( + count => $q->param('points_count'), + filter => $q->param('points_filter'), + code => sub { + my $point = shift; + push @poly_points, $point; + }, + array => \@points, + ); + + die "hum?" unless $#poly_points == $points - 1; + + $map->add_polyline( points => [ @poly_points ] ) if @poly_points; + + my $placemarks = filter_array( + count => $q->param('placemark_count'), + filter => $q->param('placemark_filter'), + code => sub { + my $placemark = shift; + $map->add_marker( %$placemark, noformat => 1 ); + }, + array => \@placemarks, + ); + my ( $map_div, $map_script ); ( $head, $map_div, $map_script ) = $map->render; - $html .= join('', qq{ -$points points from }, $q->param('trace'), qq{ showing }, - $#points > 0 ? $#points + 1 . ' points' . ( $placemarks ? ' and ' : '' ) : '', + $html .= join('', +$#points + 1, ' points from ', $q->param('trace'), ' showing ', + $points ? $points . ' points' . ( $placemarks ? ' and ' : '' ) : '', $placemarks ? $placemarks . ' placemarks' : '', qq{ $map_div