ユーザーにとって使いやすく、すべての住所情報を 1 つの入力ボックスにまとめるというあなたの考えに完全に同意します。ただし、各ユーザーが入力する情報は多少異なる場合があり、すべてのケースをカバーするアルゴリズムを考え出すことは非常に困難です。最善の策は、誰かがすでにこれを行っているかどうかを確認することです。あなたが言及したように、Google は行っています。幸いなことに、彼らはまさにそのような問題のための API を持っています。
Google マップ ジオコーダー ( https://developers.google.com/maps/documentation/geocoding/#GeocodingRequests ) を使用する場合、基本的に住所のように見えるものなら何でも渡すことができ、よく構造化された住所が返されます。結果。
Google の例: https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple
別の例 - 白い家を調べる: 次の URL をブラウザに入力します:
http://maps.googleapis.com/maps/api/geocode/json?address=1600%20pennsylvania%20ave%20washongton%20dc&sensor=false (注API の許容範囲を示すために、ここでは意図的にスペルを間違えています)。
API 呼び出しは非常に便利な JSON オブジェクトを返します。
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Pennsylvania Ave NW",
"short_name" : "Pennsylvania Ave NW",
"types" : [ "route" ]
},
{
"long_name" : "Washington",
"short_name" : "Washington",
"types" : [ "locality", "political" ]
},
{
"long_name" : "District of Columbia",
"short_name" : "DC",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "20502",
"short_name" : "20502",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Pennsylvania Ave NW, Washington, DC 20502, USA",
"geometry" : {
"location" : {
"lat" : 38.89767770,
"lng" : -77.03651700000002
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 38.89902668029149,
"lng" : -77.03516801970852
},
"southwest" : {
"lat" : 38.89632871970850,
"lng" : -77.03786598029153
}
}
},
"partial_match" : true,
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Pennsylvania Ave NW",
"short_name" : "Pennsylvania Ave NW",
"types" : [ "route" ]
},
{
"long_name" : "Washington",
"short_name" : "Washington",
"types" : [ "locality", "political" ]
},
{
"long_name" : "District of Columbia",
"short_name" : "DC",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "20500",
"short_name" : "20500",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Pennsylvania Ave NW, Washington, DC 20500, USA",
"geometry" : {
"location" : {
"lat" : 38.89871490,
"lng" : -77.03765550
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 38.90006388029150,
"lng" : -77.03630651970849
},
"southwest" : {
"lat" : 38.89736591970851,
"lng" : -77.03900448029150
}
}
},
"partial_match" : true,
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Pennsylvania Ave NW",
"short_name" : "Pennsylvania Ave NW",
"types" : [ "route" ]
},
{
"long_name" : "Washington",
"short_name" : "Washington",
"types" : [ "locality", "political" ]
},
{
"long_name" : "District of Columbia",
"short_name" : "DC",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "20004",
"short_name" : "20004",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Pennsylvania Ave NW, Washington, DC 20004, USA",
"geometry" : {
"location" : {
"lat" : 38.89549710,
"lng" : -77.03008090000002
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 38.89684608029150,
"lng" : -77.02873191970852
},
"southwest" : {
"lat" : 38.89414811970850,
"lng" : -77.03142988029153
}
}
},
"partial_match" : true,
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Pennsylvania Ave SE",
"short_name" : "Pennsylvania Ave SE",
"types" : [ "route" ]
},
{
"long_name" : "Hill East",
"short_name" : "Hill East",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Washington",
"short_name" : "Washington",
"types" : [ "locality", "political" ]
},
{
"long_name" : "District of Columbia",
"short_name" : "DC",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "20003",
"short_name" : "20003",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Pennsylvania Ave SE, Washington, DC 20003, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 38.87865290,
"lng" : -76.98170180
},
"southwest" : {
"lat" : 38.87865220,
"lng" : -76.98170229999999
}
},
"location" : {
"lat" : 38.87865290,
"lng" : -76.98170180
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 38.88000153029150,
"lng" : -76.98035306970850
},
"southwest" : {
"lat" : 38.87730356970850,
"lng" : -76.98305103029151
}
}
},
"partial_match" : true,
"types" : [ "street_address" ]
}
],
"status" : "OK"
}