perl と json を使用して、Google ジオコーダーから情報を取得するスクリプトを作成しました。以下の例では、空港の略語を使用して住所情報を取得しています。
use JSON;
use LWP::Simple;
my $geo_url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=PHL";
my $response = get($geo_url);
my $json = decode_json($response);
my $location = $json->{results}[0]->{geometry}->{location};
my $address = $json->{results}[0]->{formatted_address};
print "<br />Latitude: ".$location->{lat}." Longiude: ".$location->{lng};
print "<br />Address: ".$address;
以下を使用して、address_component 配列の個々の部分を取得できます。
$json->{results}[0]{address_components}[0]->{short_name};
$json->{results}[0]{address_components}[1]->{short_name};
しかし、私が本当にやりたいのは、都市、州、郵便番号 (postal_code) を取得することです。これを行うには、address_components をループして、次のように実行する必要があります。
for (keys $json->{results}[0]{address_components}) {
if ($json->{results}[0]{address_components}[$i]->types[0] eq "postal_code") {
print "Zip: ".$json->{results}[0]{address_components}[$i]->{short_name};
}
}
明らかにこれは有効なコードではありませんが、何をしようとしているのかを説明したかったのです。仕事に行けないループです。多くの構成を試しましたが、配列項目の長さが 6 であっても、配列の長さは 1 のままです。