package TestGoogleMaps; # JSON-RPC service package # calculate a distance between 2 points. # # See also : http://www2.neweb.ne.jp/wd/nobuaki/New_Homepage/okinawa703.htm # # copyright : makamaka[at]donzoko.net # license : same as Perl's one use strict; use constant PI => 3.14159265358979; sub echo { my $server = shift; my ($arg) = @_; return sprintf("%.3f (km)", distance( @$arg )); } sub distance { my ($d1, $d2) = @_; my ($latA, $lonA, $latB, $lonB) = ($d1->{y}, $d1->{x}, $d2->{y}, $d2->{x}); my $pA = geographic2geocentric($latA); my $pB = geographic2geocentric($latB); my $cosDelta = cos($pA) * cos($pB) * cos(($lonA - $lonB) * PI / 180) + sin($pA) * sin($pB); my $sinDelta = sqrt(1 - $cosDelta ** 2); my $rad = atan2($sinDelta, $cosDelta); $rad * 6369; # (km) } sub geographic2geocentric { my ($lat) = @_; return ($lat * PI / 180) - ((11.55/60) * PI / 180) * sin(2 * $lat * PI / 180); } 1;