1 |
#!/usr/bin/perl |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use HTML::GoogleMaps; |
7 |
use CGI::Carp qw/fatalsToBrowser/; |
8 |
use Data::Dump qw/dump/; |
9 |
|
10 |
# http://localhost/ |
11 |
my $map_key = 'ABQIAAAAVQ5szt9Jd8ws6vgfVQOEmhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ1cKf0DwFJcwtpESJEI0hL8QgtYg'; |
12 |
|
13 |
my $trace = '/home/dpavlin/x/openmoko/gps/foo.loc'; |
14 |
|
15 |
my $map = HTML::GoogleMaps->new(key => $map_key); |
16 |
#$map->center(point => "Zagreb, Hrvatska"); |
17 |
|
18 |
sub deg { |
19 |
my $a = shift; |
20 |
if ( $a =~ m/^(\d+)(\d\d\.\d\d+)$/ ) { |
21 |
return sprintf("%1.6f", $1 + ( $2 / 60 )); |
22 |
} else { |
23 |
warn "## skipped $a\n"; |
24 |
return; |
25 |
} |
26 |
} |
27 |
|
28 |
my $got_it; |
29 |
|
30 |
sub recalc { |
31 |
my ( $lat, $lat_ns, $lon, $lon_ew ) = @_; |
32 |
|
33 |
$lat = -$lat if $lat_ns eq 'S'; |
34 |
$lon = -$lon if $lon_ew eq 'W'; |
35 |
|
36 |
return if ( $got_it->{ $lat . $lon }++ ); |
37 |
|
38 |
$lat = deg( $lat ) || return; |
39 |
$lon = deg( $lon ) || return; |
40 |
|
41 |
warn "## $lon $lat\n"; |
42 |
|
43 |
return [ $lon, $lat ]; |
44 |
} |
45 |
|
46 |
open(my $fh, '<', $trace) || die "can't open $trace: $!"; |
47 |
while( <$fh> ) { |
48 |
if ( m/\$GPRMC/ ) { |
49 |
chomp; |
50 |
my @a = split(/,/,$_); |
51 |
|
52 |
next unless $#a = 12; |
53 |
|
54 |
warn "## [$#a] $_\n"; |
55 |
|
56 |
my ( undef, $time, $validity, $lat, $lat_ns, $lon, $lon_ew, $speed, $course, $date, $var, $var_ew ) = @a; |
57 |
|
58 |
next unless $validity eq 'A'; |
59 |
|
60 |
my $point = recalc( $lat, $lat_ns, $lon, $lon_ew ) || next; |
61 |
|
62 |
$map->add_marker( |
63 |
point => $point, |
64 |
html => "Time: $time<br/>Validity: $validity</br>Lat: $lat $lat_ns<br/>Lon: $lon $lon_ew<br/>Speed: $speed<br/>Course: $course", |
65 |
) if $validity eq 'A'; |
66 |
} |
67 |
} |
68 |
close($fh); |
69 |
|
70 |
#$map->zoom(10); |
71 |
#$map->v2_zoom(0); |
72 |
$map->controls("large_map_control", "map_type_control"); |
73 |
$map->map_type('hybrid'); |
74 |
|
75 |
my ($head, $map_div, $map_script) = $map->render; |
76 |
|
77 |
print "Content-type: text/html\n\r\n\r"; |
78 |
print qq{ |
79 |
<html> |
80 |
<title>Read GPS - NMEA sentence and display it on GoogleMaps</title> |
81 |
<head>$head</head> |
82 |
<body> |
83 |
$map_div |
84 |
|
85 |
$map_script |
86 |
<a href="http://aprs.gids.nl/nmea/">GPS - NMEA sentence information</a> |
87 |
</body> |
88 |
</html> |
89 |
}; |