【问题标题】:How to get City / State / Country for Mapbox using reverse geocoding?如何使用反向地理编码获取 Mapbox 的城市/州/国家/地区?
【发布时间】:2015-03-03 17:09:53
【问题描述】:

我已经阅读了这个 API 并根据一些示例测试了很多坐标 https://www.mapbox.com/developers/api/geocoding/

是一个提供大量信息的示例,问题是没有简单的方法可以仅获取我能够找到的城市、州和国家/地区。

http://api.tiles.mapbox.com/v4/geocode/mapbox.places/-114.0701,51.0495.json?access_token=pk.eyJ1IjoibWFyaXNhZmx5bm4iLCJhIjoibG9JcmhrbyJ9.yDc_eDeDW2DeM_JVSQPp7g

我可以得到这样的结果

麦克莱恩,22102,弗吉尼亚州,美国

旧金山,94103,加利福尼亚,美国

伦敦,SW1H6,英国威斯敏斯特市

旧金山,94114,加利福尼亚,美国

珀斯,6053,西澳大利亚,澳大利亚

问题是我必须弄清楚如何解析每个国家/地区的邮政编码(不合理)。

我觉得http://api.tiles.mapbox.com/v4/geocode/mapbox.places/ 可能是错误的端点。我曾一度找到http://api.tiles.mapbox.com/v4/geocode/mapbox.places-country-v1/,但它只会返回国家名称。

任何指导如何从 Mapbox 反向地理编码 api 查找中获取城市/州/国家/地区数据?

【问题讨论】:

    标签: geocoding mapbox


    【解决方案1】:

    我最终根据 mapbox 返回的内容提出了这个函数。根节点给出城镇/城市,然后给出它周围的上下文。这是一个检查哪个上下文是邮政编码并将其排除的问题,同时重建字符串。

                    //builds proper format of location string based on mapbox data. city,state/province,country
                    function parseReverseGeo(geoData) {
                        // debugger;
                        var region, countryName, placeName, returnStr;
                        if(geoData.context){
                            $.each(geoData.context, function(i, v){
                                if(v.id.indexOf('region') >= 0) {
                                    region = v.text;
                                }
                                if(v.id.indexOf('country') >= 0) {
                                    countryName = v.text;
                                }
                            });
                        }
                        if(region && countryName) {
                            returnStr = region + ", " + countryName;
                        } else {
                            returnStr = geoData.place_name;
                        }
                        return returnStr;
                    }
    

    【讨论】:

      【解决方案2】:

      同上。当我使用地理编码器结果数据设置输入字段时,我将使用这种格式。

              placeName = geoData.place_name.split(',');
              $('#addressLine1').val(placeName[1]);
      
                  if(v.id.indexOf('region') >= 0) {
                      region = v.text;
                      $('input[name="vstate"]').val(region);
                  }
                  if(v.id.indexOf('postcode') >= 0) {
                      postcode = v.text;
                      $('input[name="vpostalCode"]').val(postcode);
                  }
                  if(v.id.indexOf('place') >= 0) {
                      city = v.text;
                      $('input[name="vcity"]').val(city);
                  }               
                  if(v.id.indexOf('country') >= 0) {
                      countryName = v.text;
                      if (countryName.indexOf('United States') > 0) {
                          $('input[name="vcountry"]').val("US");
                      }
                      if (countryName.indexOf('Canada') > 0) {
                          $('input[name="vcountry"]').val("CA");
                      }
                      else {
                          $('input[name="vcountry"]').val("US");
                      }
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-12
        相关资源
        最近更新 更多