【问题标题】:Country name from latitude and longitude来自经纬度的国名
【发布时间】:2012-05-28 21:50:03
【问题描述】:

这个问题可能看起来很熟悉:我有一个地方的纬度和经度。我需要得到国家的名字。我知道我必须为此使用reverse geo coding。但我的问题是,有时它会返回地区或国家/地区的缩写形式(例如 US 代表美国或 CA 代表加利福尼亚)。有什么方法可以得到国家的全名?我无法通过这个简短的表格与我预先存储的国家数据库执行匹配操作。

我已经通过thisthis。但这对我的问题没有多大帮助。

【问题讨论】:

    标签: google-maps google-api latitude-longitude reverse-geocoding


    【解决方案1】:

    地理编码器响应通常会返回多个results,其中包括街角、十字路口、县和其他替代表示名称。我发现results[0]是最好的描述。

    诀窍是在结果中搜索“国家”。然后就可以检索到long_name了。

    Click on the map

      function getCountry(latLng) {
        geocoder.geocode( {'latLng': latLng},
          function(results, status) {
            if(status == google.maps.GeocoderStatus.OK) {
              if(results[0]) {
                for(var i = 0; i < results[0].address_components.length; i++) {
                  if(results[0].address_components[i].types[0] == "country") {
                    alert(results[0].address_components[i].long_name);
                  }
                }
              }
              else {
                alert("No results");
              }
            }
            else {
              alert("Status: " + status);
            }
          }
        );
      }
    

    【讨论】:

    • 非常感谢先生。我只是落后了一天。您的回答节省了很多时间
    • 您好,我已尝试使用您的代码获取街道名称。我已经用“street_address”替换了类型中的“国家”,但它似乎不起作用。你有机会帮我解决这个问题吗?谢谢!
    • 找到了。我只需要用“路线”替换“国家”:)
    【解决方案2】:

    JSON 数组通常包括 long_nameshort_name。您应该能够同时提取两者...

    【讨论】:

      【解决方案3】:

      这是一个适用于谷歌街道地图和开放街道地图的 JSON/XML 解析器。

      (唯一的问题是它需要一个 JSON 或 XML 对象作为“回复”,它在 3 版谷歌和 0.6 版开放街道地图上测试过,而且效果很好)

      注意:它返回一个对象 location.lat 或 location.lon 你也可以让它返回你想要的任何其他字段。

      JSON.parse(text) // 其中 text 是来自 google 或打开的街道地图的回复 XML.parse(text) // 您可以自己制作将回复转换为 XML 或使用正则表达式对其进行解析。如果有人有正则表达式版本来解析文本回复,这也可能会有所帮助。

      // Parser(ajax reply object, google/open, json/xml); 
      // takes the reply from google maps or open street maps and creates an object with location[lat/lon] 
      function Parser(reply, provider, type) {
        var location = {};
        if(reply != null) {
          if(provider == "google") { // Google Street Maps
            switch(type) {
            case "xml":
              location["lat"] = reply.getElementsByTagName("lat")[0].textContent; 
              location["lon"] = reply.getElementsByTagName("lng")[0].textContent; 
              break;
            default: // json
              location["lat"] = reply.results[0].geometry.location.lat;
              location["lon"] = reply.results[0].geometry.location.lng;
            }
          }
          else { // Open Street Maps
            switch(type) {
            case "xml":
              location["lat"] = reply.getElementsByTagName("place")[0].getAttribute("lat"); 
              location["lon"] = reply.getElementsByTagName("place")[0].getAttribute("lon"); 
              break;
            default: // json
              location["lat"] = reply[0].lat;
              location["lon"] = reply[0].lon;
            }
          }
        }
        return location;
      }
      

      【讨论】:

        【解决方案4】:
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    $.post("https://maps.googleapis.com/maps/api/geocode/json?latlng=" + position.coords.latitude + "," + position.coords.longitude + "&sensor=false", function (result) {
                        for (var i = 0; i < result['results'][0]['address_components'].length; i++) {
                            if (result['results'][0]['address_components'][i]['types'][0] == "country") {
                                alert(result['results'][0]['address_components'][i]['long_name']);
                            }
                        }
                    });
                });
            }
        }
        getLocation();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-16
          • 1970-01-01
          • 2015-07-14
          • 1970-01-01
          • 2011-05-28
          • 1970-01-01
          相关资源
          最近更新 更多