Perlでは、どのようにして任意のアドレスからlatとlongを取得しますか?外部APIを使用しても問題ありません。
6432 次
3 に答える
3
これは、任意のアドレスの緯度と経度を取得するための簡単なperl関数です。CPANおよびGoogleのGeocodeAPIのLWP::SimpleとJSONを使用します。完全なデータが必要な場合は、jsonまたはxmlのいずれかを実行できます。これは、のjsonを使用し、latとlongを取得して返します。
use strict;
use LWP::Simple; # from CPAN
use JSON qw( decode_json ); # from CPAN
sub getLatLong($){
my ($address) = @_;
my $format = "json"; #can also to 'xml'
my $geocodeapi = "https://maps.googleapis.com/maps/api/geocode/";
my $url = $geocodeapi . $format . "?sensor=false&address=" . $address;
my $json = get($url);
my $d_json = decode_json( $json );
my $lat = $d_json->{results}->[0]->{geometry}->{location}->{lat};
my $lng = $d_json->{results}->[0]->{geometry}->{location}->{lng};
return ($lat, $lng);
}
于 2012-12-05T04:50:48.117 に答える
2
Geo :: Coder :: Manyを使用して、さまざまなサービスを比較し、最も正確であると思われるサービスを見つけることができます。田舎と都会の住所が混在する中で、私はそれ(米国)で幸運に恵まれました。
use Geo::Coder::Bing;
use Geo::Coder::Googlev3;
use Geo::Coder::Mapquest;
use Geo::Coder::OSM;
use Geo::Coder::Many;
use Geo::Coder::Many::Util qw( country_filter );
### Geo::Coder::Many object
my $geocoder_many = Geo::Coder::Many->new( );
$geocoder_many->add_geocoder({ geocoder => Geo::Coder::Googlev3->new });
$geocoder_many->add_geocoder({ geocoder => Geo::Coder::Bing->new( key => 'GET ONE' )});
$geocoder_many->add_geocoder({ geocoder => Geo::Coder::Mapquest->new( apikey => 'GET ONE' )});
$geocoder_many->add_geocoder({ geocoder => Geo::Coder::OSM->new( sources => 'mapquest' )});
$geocoder_many->set_filter_callback(country_filter('United States'));
$geocoder_many->set_picker_callback('max_precision');
for my $location (@locations) {
my $result = $geocoder_many->geocode({ location => $location });
}
于 2012-12-05T15:30:32.527 に答える
1
Google Geocoding API のラッパー モジュールGeo::Coder::Googleがあります。
#!/usr/bin/env perl
use strict;
use utf8;
use warnings qw(all);
use Data::Printer;
use Geo::Coder::Google;
my $geocoder = Geo::Coder::Google->new(apiver => 3);
my $location = $geocoder->geocode(location => 'Hollywood and Highland, Los Angeles, CA');
p $location->{geometry}{location};
このコードは以下を出力します。
\ {
lat 34.101545,
lng -118.3386871
}
一般的には、 CPAN テスターサービスによってサポートされている既製の CPAN モジュールを使用することをお勧めします。そのため、API が壊れた場合でも、簡単に見つけて報告できます。
于 2012-12-05T13:41:24.800 に答える