4

我有一个地址字符串,例如“12 Swiss Ave, Gonville, Wanganui, New Zealand”

鉴于此地址,我需要确定该地址是在新西兰的北岛还是南岛之内。

如果我将地址传递给谷歌地图 api,我得到的数据如下,不幸的是,虽然它没有告诉我它在哪个岛。那么,我该如何检查呢?

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "12",
                    "short_name": "12",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "Swiss Avenue",
                    "short_name": "Swiss Ave",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Gonville",
                    "short_name": "Gonville",
                    "types": [
                        "sublocality",
                        "political"
                    ]
                },
                {
                    "long_name": "Wanganui",
                    "short_name": "Wanganui",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Manawatu-Wanganui",
                    "short_name": "Manawatu-Wanganui",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "New Zealand",
                    "short_name": "NZ",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "4501",
                    "short_name": "4501",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "12 Swiss Avenue, Gonville, Wanganui 4501, New Zealand",
            "geometry": {
                "location": {
                    "lat": -39.9444245,
                    "lng": 175.0239575
                },
                "location_type": "ROOFTOP",
                "viewport": {
                    "northeast": {
                        "lat": -39.9430755197085,
                        "lng": 175.0253064802915
                    },
                    "southwest": {
                        "lat": -39.9457734802915,
                        "lng": 175.0226085197085
                    }
                }
            },
            "types": [
                "street_address"
            ]
        }
    ],
    "status": "OK"
}
4

2 回答 2

3

一种选择是获取两个岛屿的多边形,并对地理位置进行多边形分析。

使用由 KMLgeoxml3定义的多边形的示例

概念证明(使用 geoxml3 和来自 gadm.org 的修改后的 KML)

============ 使用融合表的旧选项,不再有效 =============

使用 FusionTables 的示例注意:不再有效,Fusion Layers 在 2019 年 12 月被拒绝)

概念证明(使用融合表中的自然地球数据集和“州到岛”地图)(注意:也不再有效)

于 2013-04-08T00:16:11.453 回答
0

很好地使用谷歌地图的API ..

您可以从说Places Autocomplete获得新西兰北岛的 placeId ,即ChIJhXgO3F4Uam0RdyMcnMutKMo

然后使用 geocode 库获取边界,并使用contains 函数测试其他 lat/lng 是否在这些边界内。

const geocode = async placeId => {
  const geocoder = new window.google.maps.Geocoder();
  return new Promise((resolve, reject) => {
    geocoder.geocode(
      {
        placeId
      },
      (results, status) => {
        resolve(results[0]);
      }
    );
  });
};

const placeDetails = await geocode('ChIJhXgO3F4Uam0RdyMcnMutKMo');
const somePlace = getPlaceDetails(someOtherPlaceId);

const isInNorthIsland = placeDetails.geometry.bounds.contains(somePlace.geometry.location);

于 2021-05-26T07:40:40.927 回答