我从第 3 方 API 获取城市名称。该 API 不会返回规范化的城市名称。例如,有时是旧金山
,有时是加利福尼亚州旧金山
,有时是美国旧金山
。我不需要街道地址等。我只需要将城市名称规范化为统一的名称。我正在寻找可以帮助我满足此要求的服务。
P.S:它不是移动应用程序,而是网络应用程序,并且位置不是来自浏览器。
最佳答案
您可以使用 Google Maps API 获取标准化地址:
1) 从以下位置获取结果:https://maps.googleapis.com/maps/api/geocode/json?address=san_francisco
{
"results" : [
{
"address_components" : [
{
"long_name" : "San Francisco",
"short_name" : "SF",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Condado San Francisco",
"short_name" : "Condado San Francisco",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Estados Unidos",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "San Francisco, California, EE. UU.",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.9298239,
"lng" : -122.28178
},
"southwest" : {
"lat" : 37.6398299,
"lng" : -123.173825
}
},
"location" : {
"lat" : 37.7749295,
"lng" : -122.4194155
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 37.812,
"lng" : -122.3482
},
"southwest" : {
"lat" : 37.70339999999999,
"lng" : -122.527
}
}
},
"place_id" : "ChIJIQBpAG2ahYAR_6128GcTUEo",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
2) 从$data['results'][0]['address_components'][0]['long_name']
中提取城市名称。
You should check that the returned content contains the described fields and the
$data['status']
is "OK".
来源:https://developers.google.com/maps/documentation/geocoding/intro
关于node.js - 如何从许多相似的名称中标准化城市名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39583815/