2

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

我已经通过this,this。但对我的问题没有太大的帮助。

回答

5

地理编码器响应通常返回几个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); 
     } 
     } 
    ); 
    } 
+1

非常感谢主席先生。我只有一天的时间。您的回答节省了大量的时间 – arjuncc

+0

嗨,我试图使用您的代码获取街道名称。我用“street_address”替换了“country”类型,但似乎不起作用。有没有机会可以帮助我呢?谢谢! – inadcod

+0

找到了。我只需用“路线”替换“国家”:) – inadcod

2

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

+0

感谢您的答复先生 – arjuncc

0

这里是一个JSON/XML解析器,无论对于谷歌街道地图和开放街道地图作品。

(唯一的问题是,它需要一个JSON或XML对象作为“回应”,其对第3版谷歌和0.6开放街道地图测试,它的工作好)

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

JSON.parse(文本)//其中文本是从谷歌或开街的答复映射 XML.parse(文本)//你可以让自己的回复转换为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();