在不使用 UI 的情况下使用 Google Maps SDK 解析完整地址
Resolve complete address using Google Maps SDK without UI
我有一个地址列表,没有格式化,不一定写对
我想迭代这些损坏的地址字符串,并使用 Google Maps SDK
之一接收结构更完整的地址
基本上我有两个问题:
- 哪个 SDK 最适合这项任务? (有40个名单)
- 没有UI我该如何使用它? (我看到的所有解决方案都包括 UI 和一个搜索框)
如果您实际使用的是 google-maps SDK,您可能正在寻找 geocoding API
它可以在没有 UI 的情况下使用,因为它也可以通过此端点作为 json 获取:
https://maps.googleapis.com/maps/api/geocode/outputFormat?parameters
例如:
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,
+Mountain+View,+CA&key=YOUR_API_KEY
或通过 JS api:
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': 'Mountain View, CA'}, function(results, status) {
if (status == 'OK') {
console.log(results);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
结果:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"plus_code": {
"compound_code": "CWC8+W5 Mountain View, California, United States",
"global_code": "849VCWC8+W5"
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
我有一个地址列表,没有格式化,不一定写对
我想迭代这些损坏的地址字符串,并使用 Google Maps SDK
之一接收结构更完整的地址基本上我有两个问题:
- 哪个 SDK 最适合这项任务? (有40个名单)
- 没有UI我该如何使用它? (我看到的所有解决方案都包括 UI 和一个搜索框)
如果您实际使用的是 google-maps SDK,您可能正在寻找 geocoding API
它可以在没有 UI 的情况下使用,因为它也可以通过此端点作为 json 获取:
https://maps.googleapis.com/maps/api/geocode/outputFormat?parameters
例如:
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,
+Mountain+View,+CA&key=YOUR_API_KEY
或通过 JS api:
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': 'Mountain View, CA'}, function(results, status) {
if (status == 'OK') {
console.log(results);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
结果:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"plus_code": {
"compound_code": "CWC8+W5 Mountain View, California, United States",
"global_code": "849VCWC8+W5"
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}