Google Maps:如何获取国家/地区/省/自治区/直辖市/自治区?

问题描述 投票:107回答:9

我需要基于我拥有的经/纬度值的集合来列出国家,州和城市。我需要以保留层次结构且无重复的方式存储此信息(例如“美国”,“美国”和“美利坚合众国”是同一国家/地区;我只希望该国家/地区的一个实例在我的数据库中) 。

这可能与Google Map API有关吗?

google-maps geolocation google-maps-api-3 geocoding
9个回答
133
投票

您正在寻找的被称为reverse geocoding。 Google通过Google Geocoding API提供了服务器端反向地理编码服务,您应该可以在项目中使用它。

这是对以下请求的响应如下:

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

响应:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
    "address_components": [ {
      "long_name": "275-291",
      "short_name": "275-291",
      "types": [ "street_number" ]
    }, {
      "long_name": "Bedford Ave",
      "short_name": "Bedford Ave",
      "types": [ "route" ]
    }, {
      "long_name": "New York",
      "short_name": "New York",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Brooklyn",
      "short_name": "Brooklyn",
      "types": [ "administrative_area_level_3", "political" ]
    }, {
      "long_name": "Kings",
      "short_name": "Kings",
      "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" ]
    }, {
      "long_name": "11211",
      "short_name": "11211",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 40.7142298,
        "lng": -73.9614669
      },
      "location_type": "RANGE_INTERPOLATED",
      "viewport": {
        "southwest": {
          "lat": 40.7110822,
          "lng": -73.9646145
        },
        "northeast": {
          "lat": 40.7173774,
          "lng": -73.9583193
        }
      }
    }
  },

  ... Additional results[] ...

您还可以通过简单地用请求URI中的xml代替json来选择以xml而不是json接收响应:

http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false

据我所知,Google还将为地址部分返回相同的名称,尤其是对于诸如国家名称和城市名称的高级名称。不过,请记住,尽管对于大多数应用程序结果都是非常准确的,但是您仍然会发现偶尔的拼写错误或含糊的结果。


21
投票

您在这里有一个基本的答案:Get city name using geolocation

但是对于您要寻找的东西,我会推荐这种方式。

[仅当您还需要Administrative_area_level_1时,才能为美国德克萨斯州的巴黎和法国法兰西岛的巴黎存储不同的东西,并提供手动的备用广告:

-

米哈尔(Michal)的方式存在问题,因为它得到的是第一个结果,而不是一个特定的结果。他使用结果[0]。我认为合适的方式(我刚刚修改了他的代码)是仅获取类型为“ locality”的结果,该结果始终存在,即使在最终的手动回退中,以防浏览器不支持地理位置。

他的方式:获取的结果不同于使用http://maps.googleapis.com/maps/api/geocode/json?address=bucharest&sensor=false比使用http://maps.googleapis.com/maps/api/geocode/json?latlng=44.42514,26.10540&sensor=false(按名称搜索/按纬度搜索)

这种方式:相同的获取结果。

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Reverse Geocoding</title> 

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
  var geocoder;

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} 
//Get the latitude and the longitude;
function successFunction(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    codeLatLng(lat, lng)
}

function errorFunction(){
    alert("Geocoder failed");
}

  function initialize() {
    geocoder = new google.maps.Geocoder();



  }

  function codeLatLng(lat, lng) {

    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      //console.log(results);
        if (results[1]) {
        var indice=0;
        for (var j=0; j<results.length; j++)
        {
            if (results[j].types[0]=='locality')
                {
                    indice=j;
                    break;
                }
            }
        alert('The good number is: '+j);
        console.log(results[j]);
        for (var i=0; i<results[j].address_components.length; i++)
            {
                if (results[j].address_components[i].types[0] == "locality") {
                        //this is the object you are looking for City
                        city = results[j].address_components[i];
                    }
                if (results[j].address_components[i].types[0] == "administrative_area_level_1") {
                        //this is the object you are looking for State
                        region = results[j].address_components[i];
                    }
                if (results[j].address_components[i].types[0] == "country") {
                        //this is the object you are looking for
                        country = results[j].address_components[i];
                    }
            }

            //city data
            alert(city.long_name + " || " + region.long_name + " || " + country.short_name)


            } else {
              alert("No results found");
            }
        //}
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }
</script> 
</head> 
<body onload="initialize()"> 

</body> 
</html> 

4
投票

我用这个问题作为自己解决方案的起点。由于它的代码比tabacitu的代码小[

依赖关系:

  • underscore.js
  • https://github.com/estebanav/javascript-mobile-desktop-geolocation
© www.soinside.com 2019 - 2024. All rights reserved.