【问题标题】:How to also get the name of the city when geocoding?地理编码时如何获取城市名称?
【发布时间】:2020-05-14 07:28:13
【问题描述】:

我正在使用Mapbox Geocoding API 来查找用户输入提供的地点的纬度和经度。这很好用。我还想显示该位置所在城市的名称。

这是一个示例请求,它搜索“70176”,德国的邮政编码:

https://api.mapbox.com/geocoding/v5/mapbox.places/70176.json?fuzzyMatch=true&language=en&access_token=redacted

这就是答案:

{
   "type":"FeatureCollection",
   "query":[
      "70176"
   ],
   "features":[
      {
         "id":"postcode.12480061547829920",
         "type":"Feature",
         "place_type":[
            "postcode"
         ],
         "relevance":1,
         "properties":{

         },
         "text_en":"70176",
         "place_name_en":"70176, Stuttgart, Baden-Württemberg, Germany",
         "text":"70176",
         "place_name":"70176, Stuttgart, Baden-Württemberg, Germany",

   [...]

}

如您所见,它确实提供了包含城市(“斯图加特”)的完整地址,但它没有分开。您可以通过在查询中包含 &types=place 来指定您只查找城市,但它也只接受城市名称作为输入。

如何在不进行两次 API 调用的情况下获取纬度、经度和城市名称?

【问题讨论】:

    标签: mapbox geocoding


    【解决方案1】:

    您实际上从您提出的请求中获得了所有这些信息:

    包围盒的中心坐标,包围城市在特征 JSON 对象的“中心”项中返回。您从要素对象的“place_name”项中获得的城市名称。您必须解析字符串并用逗号将其拆分,然后选择返回数组的第二项以获取城市名称。

    {
    "type": "FeatureCollection",
    "query": [
        "70176"
    ],
    "features": [
        {
            "id": "postcode.12480061547829920",
            "type": "Feature",
            "place_type": [
                "postcode"
            ],
            "relevance": 1,
            "properties": {},
            "text_en": "70176",
            "place_name_en": "70176, Stuttgart, Baden-Württemberg, Germany",
            "text": "70176",
            "place_name": "70176, Stuttgart, Baden-Württemberg, Germany",
            "bbox": [
                9.155781,
                48.77099,
                9.169018,
                48.784448
            ],
            "center": [
                9.16,
                48.78
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    9.16,
                    48.78
                ]
            },
            "context": [
                {
                    "id": "place.5443458428087800",
                    "wikidata": "Q1022",
                    "text_en": "Stuttgart",
                    "language_en": "en",
                    "text": "Stuttgart",
                    "language": "en"
                },
                {
                    "id": "region.10788925313210430",
                    "short_code": "DE-BW",
                    "wikidata": "Q985",
                    "text_en": "Baden-Württemberg",
                    "language_en": "en",
                    "text": "Baden-Württemberg",
                    "language": "en"
                },
                {
                    "id": "country.10743216036480410",
                    "short_code": "de",
                    "wikidata": "Q183",
                    "text_en": "Germany",
                    "language_en": "en",
                    "text": "Germany",
                    "language": "en"
                }
            ]
        },
    

    【讨论】:

    • 如果它只是那么简单...根据查询,城市名称有时是 place_name 中的第一或第三项。
    • 如何使用“context”子JSON对象的“text_en”,其中“id”以place开头?这似乎是独一无二的。
    • 我也试过了,但它是一样的。城市并不总是第一个上下文子项。
    • 没关系,在哪个位置。只需遍历上下文 JSON 对象并查找“id”值以“place”开头的对象。
    • 好主意!不幸的是,它并不总是存在。例如“莫斯科”。
    猜你喜欢
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多