首页 文章

纬度和经度的国家名称

提问于
浏览
2

这个问题可能看起来很熟悉:我有一个地方的纬度和经度 . 我需要得到国家的名字 . 我知道我必须使用reverse geo coding . 但我的问题是,有时它会返回该地区或国家的缩写形式(例如美国代表美国,CA代表加州) . 有什么方法可以得到 full name of the country 吗?我不能通过这个简短的表格与我预存的国家数据库进行匹配操作 .

我已经经历了thisthis . 但这对我的问题没什么帮助 .

4 回答

  • 2

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

    诀窍是在结果中搜索“country” . 然后可以检索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);
            }
          }
        );
      }
    
  • 0

    JSON数组通常包括 long_nameshort_name . 你应该能够提取两个......

  • 5

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

    (唯一的问题是它需要一个JSON或XML对象作为“回复”,它在版本3谷歌和0.6开放街道 Map 上测试,它运作良好)

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

    JSON.parse(text)//其中text是来自google或开放街道 Map 的回复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;
    }
    
  • 0
    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();
    

相关问题