首页 > 解决方案 > 当我们使用 Rest API 时如何获取给定地址的县信息

问题描述

我正在使用下面的代码来获取地址信息,并且我正在获取整个地址。但我只需要给定城市的县和美国的邮编。请帮忙

function getCounty()
{

    $.ajax({
  url: 'https://geocoder.api.here.com/6.2/geocode.json',
  type: 'GET',
  dataType: 'jsonp',
  jsonp: 'jsoncallback',
  data: {
    city: 'Farmington',
    postalcode: '48335',
    country: 'usa',

    app_id: '**************************',
    app_code: '************************',
    gen: '9'
  },
  success: function (data) {
alert(JSON.stringify(data));

  }
});

}

标签: here-api

解决方案


请尝试以下代码以访问响应的县属性。希望这有帮助。

alert(data.Response.View[0].Result[0].Location.Address.County);

请检查以下请求的完整 JSON 响应。

{
  "Response": {
    "MetaInfo": {
      "Timestamp": "2019-01-09T07:08:18.912+0000"
    },
    "View": [
      {
        "_type": "SearchResultsViewType",
        "ViewId": 0,
        "Result": [
          {
            "Relevance": 1,
            "MatchLevel": "postalCode",
            "MatchQuality": {
              "City": 1,
              "PostalCode": 1
            },
            "Location": {
              "LocationId": "NT_9usvaftBv-o9T6WyVpnWRC",
              "LocationType": "area",
              "DisplayPosition": {
                "Latitude": 42.47427,
                "Longitude": -83.40889
              },
              "NavigationPosition": [
                {
                  "Latitude": 42.47427,
                  "Longitude": -83.40889
                }
              ],
              "MapView": {
                "TopLeft": {
                  "Latitude": 42.48732,
                  "Longitude": -83.4355
                },
                "BottomRight": {
                  "Latitude": 42.43954,
                  "Longitude": -83.37537
                }
              },
              "Address": {
                "Label": "48335, Farmington, MI, United States",
                "Country": "USA",
                "State": "MI",
                "County": "Oakland",
                "City": "Farmington",
                "PostalCode": "48335",
                "AdditionalData": [
                  {
                    "value": "United States",
                    "key": "CountryName"
                  },
                  {
                    "value": "Michigan",
                    "key": "StateName"
                  },
                  {
                    "value": "Oakland",
                    "key": "CountyName"
                  },
                  {
                    "value": "N",
                    "key": "PostalCodeType"
                  }
                ]
              }
            }
          }
        ]
      }
    ]
  }
}

推荐阅读