【问题标题】:Resolve complete address using Google Maps SDK without UI使用不带 UI 的 Google Maps SDK 解析完整地址
【发布时间】:2021-10-28 17:59:18
【问题描述】:

我有一个地址列表,没有格式化,也不一定写正确。

我想迭代这些损坏的地址字符串,并使用其中一个 Google Maps SDK 接收更结构化和更完整的地址

基本上我有两个问题:

  1. 哪个 SDK 最适合这项任务? (有 40 个列表)
  2. 如何在没有 UI 的情况下使用它? (我看到的所有解决方案都包括 UI 和搜索框)

【问题讨论】:

  • 由于您的输入地址可能格式错误和/或不完整,您应该使用 Places API 自动完成功能。它是适用于 Android 或 iOS 的 Places SDK 的一部分。 developers.google.com/maps/documentation/geocoding/…
  • 感谢您的评论,但我不是在寻找地点。我实际上是在寻找格式不正确的住宅地址,所以我认为地图地理定位 api 做得很好
  • 地址是地方仅供参考

标签: javascript typescript google-maps google-maps-api-3


【解决方案1】:

如果您实际使用的是 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"
}

【讨论】:

  • 重要的是要知道这并没有预期的那么好,经过几个小时的磨练后,我意识到在某些情况下,我必须在输入上做一些体力劳动才能得到我想要的结果:|
  • 另外,当我从后端运行它时,我使用了 https API 并且没有导入 JS 库,因为它包含 html 元素引用以及其他 UI 内容。如果你知道原始的js lib..请告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 2018-06-27
相关资源
最近更新 更多