Google地图地理编码器针对完整的城镇名称(包括县,州)返回多个结果

时间:2014-01-04 10:51:34

标签: google-maps google-geocoder google-geocoding-api

我有以下地址:

“切尔西,纽约,纽约,美国”

在上述地址中:

City ='Chelsea' 县=“纽约” 州=“纽约州” 国家=“美国”

当我将此地址传递给Google的Geocoder API时,它会返回多个结果,而我期待一个结果,因为城镇名称的每个部分(名称,县,州,国家/地区)都在地址中指定。

以下是代码段:

(假设已包含所需文件)

var geocoder = new google.maps.Geocoder();

geocoder.geocode({ "address": "Chelsea, New York, NY, USA" }, function (results, status) {

                    if (status == google.maps.GeocoderStatus.OK) {
                  if (results.length == 1) {                       
                      // do something
                        }
                      else{
                         // do something
                        }

感谢您的帮助。谢谢。

1 个答案:

答案 0 :(得分:2)

地理编码器找到2个匹配字符串“Chelsea,New York,NY,USA”

[ 0 ]: Chelsea, New York, NY, USA (40.7497717, -73.99769459999999)
[ 1 ]: Travis - Chelsea, Staten Island, NY, USA (40.5890113, -74.19151769999996)

您假设您将获得“单一结果,因为城镇名称的每个部分(姓名,县,州,国家/地区)都在地址中指定”无效。

第一个:

{
  "address_components": [
    {
      "long_name": "Chelsea",
      "short_name": "Chelsea",
      "types": [
        "neighborhood",
        "political"
      ]
    },
    {
      "long_name": "Manhattan",
      "short_name": "Manhattan",
      "types": [
        "sublocality",
        "political"
      ]
    },
    {
      "long_name": "New York",
      "short_name": "New York",
      "types": [
        "locality",
        "political"
      ]
    },
    {
      "long_name": "New York",
      "short_name": "New York",
      "types": [
        "administrative_area_level_2",
        "political"
      ]
    },
    {
      "long_name": "New York",
      "short_name": "NY",
      "types": [
        "administrative_area_level_1",
        "political"
      ]
    },
    {
      "long_name": "United States",
      "short_name": "US",
      "types": [
        "country",
        "political"
      ]
    }
  ],
  "formatted_address": "Chelsea, New York, NY, USA",
  "geometry": {
    "bounds": {
      "ea": {
        "d": 40.7373582,
        "b": 40.7570384
      },
      "fa": {
        "b": -74.00886300000002,
        "d": -73.98779150000001
      }
    },
    "location": {
      "nb": 40.7497717,
      "ob": -73.99769459999999
    },
    "location_type": "APPROXIMATE",
    "viewport": {
      "ea": {
        "d": 40.7373582,
        "b": 40.7570384
      },
      "fa": {
        "b": -74.00886300000002,
        "d": -73.98779150000001
      }
    }
  },
  "types": [
    "neighborhood",
    "political"
  ]
}

第二个:

{
  "address_components": [
    {
      "long_name": "Travis - Chelsea",
      "short_name": "Travis - Chelsea",
      "types": [
        "neighborhood",
        "political"
      ]
    },
    {
      "long_name": "Staten Island",
      "short_name": "Staten Island",
      "types": [
        "sublocality",
        "political"
      ]
    },
    {
      "long_name": "New York",
      "short_name": "New York",
      "types": [
        "locality",
        "political"
      ]
    },
    {
      "long_name": "Richmond",
      "short_name": "Richmond",
      "types": [
        "administrative_area_level_2",
        "political"
      ]
    },
    {
      "long_name": "New York",
      "short_name": "NY",
      "types": [
        "administrative_area_level_1",
        "political"
      ]
    },
    {
      "long_name": "United States",
      "short_name": "US",
      "types": [
        "country",
        "political"
      ]
    }
  ],
  "formatted_address": "Travis - Chelsea, Staten Island, NY, USA",
  "geometry": {
    "bounds": {
      "ea": {
        "d": 40.57709699999999,
        "b": 40.603491
      },
      "fa": {
        "b": -74.20663830000001,
        "d": -74.17552469999998
      }
    },
    "location": {
      "nb": 40.5890113,
      "ob": -74.19151769999996
    },
    "location_type": "APPROXIMATE",
    "viewport": {
      "ea": {
        "d": 40.57709699999999,
        "b": 40.603491
      },
      "fa": {
        "b": -74.20663830000001,
        "d": -74.17552469999998
      }
    }
  },
  "types": [
    "neighborhood",
    "political"
  ]
}