【问题标题】:How to normalize city name from many similar names如何从许多相似的名称中标准化城市名称
【发布时间】:2016-09-20 00:07:22
【问题描述】:

我从第 3 方 API 获取城市名称。该 API 不返回规范化的城市名称。例如,有时是San Francisco,有时是San Francisco, CA,有时是San Francisco, USA。我不需要街道地址等。我只需要将城市名称标准化为统一的东西。我正在寻找可以帮助我满足此要求的服务。

P.S:它不是移动应用程序,它的网络应用程序和位置不是来自浏览器。

【问题讨论】:

    标签: node.js location city


    【解决方案1】:

    您可以使用 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']中提取城市名称。

    您应该检查返回的内容是否包含所描述的字段并且$data['status'] 是“OK”。

    来源:https://developers.google.com/maps/documentation/geocoding/intro

    【讨论】:

      【解决方案2】:

      如果与城市名称一致,可以扫描字符串,检查是否有逗号,后面是否有忽略

      【讨论】:

      • 感谢您的建议。它不一致你:(
      猜你喜欢
      • 2021-07-10
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多