代码之家  ›  专栏  ›  技术社区  ›  ibz

GPS坐标的国家名称

  •  18
  • ibz  · 技术社区  · 15 年前

    有没有一个简单的方法来获得一个给定点所在国家的名称?

    我不太关心准确性,也不太关心政治纠纷所造成的模棱两可。只是需要一个足够好的近似值。

    5 回复  |  直到 9 年前
        1
  •  6
  •   Michael Wagner    9 年前

    编辑: 不再提供API的雅虎。

    试试雅虎! APIs .

    经纬度

    纬度和 经度可以指定为 位置参数。纬度和 经度可以用十进制表示。 度或度分秒, 有前导或尾随 方向或引导标志。如果 提供了方向,经度 可能出现在纬度之前。如果 未提供方向图, 经度可能出现在纬度之前 如果超出-90到90的范围。 否则,纬度必须出现 第一。标点符号(逗号, 度、分、秒)是 忽略。

    Examples: 
    
    •50.3 -120.5
    •50.3, -120.5
    •-120.5 50.3
    •50.3 N 120.5 W
    •120.5 W 50.3 N
    •50 18 0 -120 30 0
    •50 18 0 N 120 30 0 W
    •50° 18' 0" N 120° 30' 0" W
    

    响应元素将为您提供国家,以及许多其他 elements .

    不要忘记作为参数gflgs=r发送,以进行反向地理编码。如果您希望在JSON中输出,那么也发送参数标志=J。

        2
  •  5
  •   ibz    15 年前

    作为参考,geonames.org也有一个不错的webservice,但是它的速率是有限的(在我的例子中这是一个问题,因为我必须查找大量的坐标)。

    例子: http://ws.geonames.org/findNearbyPlaceName?lat=47.3&lng=9

        3
  •  5
  •   Tim Cooper    14 年前

    如何使用 google's reverse geo-encoding 服务?

        4
  •  1
  •   hippietrail    13 年前

    下面是一些从经纬度坐标中获取国家名称的javascript代码:


    1. 使用谷歌的地理编码服务 [jsfiddle] :

      var lookupcountrywithgoogle=函数(lat,lng){ var lat lng=新的google.maps.lat lng(lat,lng);

      var geoCoder = new google.maps.Geocoder();
      
      geoCoder.geocode({
          location: latlng
      }, function(results, statusCode) {
          var lastResult = results.slice(-1)[0];
      
          if (statusCode == 'OK' && lastResult && 'address_components' in lastResult) {
              alert('coordinates: ' + lat + ', ' + lng + '\n' + 'country: ' + lastResult.address_components.slice(-1)[0].long_name);
          } else {
              alert('coordinates: ' + lat + ', ' + lng + '\n' + 'failed: ' + statusCode);
          }
      });
      

      };

      使用Google查找国家(43.7534932,28.5743187);//将成功 lookupcountrywithgoogle(142124);//将失败

    (您需要从一个URL加载Google的javascript支持,例如 maps.google.com/maps/api/js?sensor=false&fake=.js )


    1. 使用 ws.geonames.org [jsfiddle] :

      var lookupcountrywithgeonames=函数(lat,lng){

      $.getJSON('http://ws.geonames.org/countryCode', {
          lat: lat,
          lng: lng,
          type: 'JSON'
      }, function(results) {
          if (results.countryCode) {
              alert('coordinates: ' + lat + ', ' + lng + '\n' + 'country: ' + results.countryName);
          } else {
              alert('coordinates: ' + lat + ', ' + lng + '\n' + 'failed: ' + results.status.value + ' ' + results.status.message);
          }
      });
      

      };

      使用地理名称查找国家(43.7534932,28.5743187);//将成功

      lookupcountrywithgeonames(142124);//将失败 艾斯

        5
  •  0
  •   Joachim Van der Auwera    15 年前

    您还可以查看Tinygeocoder提供的服务。

    另一种选择是从自然地球(或其他来源)下载国家地图。使用地理工具,您可以搜索该点是否在其中一个国家内。