有没有一种简单的方法可以获取给定点所在的国家/地区的名称?
我不太关心准确性,也不太关心政治争端所带来的模棱两可。只需要一个足够好的近似值。
有没有一种简单的方法可以获取给定点所在的国家/地区的名称?
我不太关心准确性,也不太关心政治争端所带来的模棱两可。只需要一个足够好的近似值。
编辑: Yahoo Where API 不再可用。
试试雅虎!API。
纬度和经度
可以为位置参数指定纬度和经度。纬度和经度可以用十进制度或度-分-秒表示,带有前导或尾随方向或前导符号。如果提供了方向,则经度可能会出现在纬度之前。如果没有提供方向,如果纬度在 -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
响应元素将为您提供国家以及许多其他元素。
不要忘记作为参数发送 gflags=R,以进行反向地理编码。如果您想要 json 格式的输出,请同时发送参数 flags=J。
使用谷歌的反向地理编码服务怎么样?
仅供参考,geonames.org 也有一个不错的 web 服务,但它的速率有限(在我的情况下这是一个问题,因为我必须查找大量坐标)。
示例:http ://ws.geonames.org/findNearbyPlaceName?lat=47.3&lng=9
您还可以查看来自 tinygeocoder 的服务。
另一种选择是从自然地球(或其他来源)下载国家地图。使用 Geotools,您可以搜索该点是否在其中一个国家/地区内。
下面是一些 JavaScript 代码,用于从纬度和经度坐标中获取国家/地区名称:
使用 Google 的地理编码服务[jsfiddle]:
var lookupCountryWithGoogle = function(lat, lng) { var latlng = new google.maps.LatLng(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);
}
});
};
lookupCountryWithGoogle(43.7534932, 28.5743187); // 将成功 lookupCountryWithGoogle(142, 124); // 将失败
(您需要从 URL 加载 Google 对此的 JavaScript 支持,例如maps.google.com/maps/api/js?sensor=false&fake=.js
)
var lookupCountryWithGeonames = function(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);
}
});
};
lookupCountryWithGeonames(43.7534932, 28.5743187); // 将会成功
lookupCountryWithGeonames(142, 124); // 会失败</p>
Geonames API 发生了一些变化。更多信息在这里:https ://www.geonames.org/export/web-services.html
该页面的一个工作示例:
curl "http://api.geonames.org/countryCode?lat=47.03&lng=10.2&username=demo"