我正试图从给定的纬度和经度中获取一个位置的名称。有没有可能像一样得到它
getNameOfLocation(经度、纬度)
例如,getNameOfLocation(48.1471904942317,11.591434478759765)-->结果慕尼黑
我不想像谷歌地图那样使用任何图形元素来显示。
有什么想法吗?感谢的帮助
var geo = Ext.create('Ext.util.Geolocation', {
autoUpdate: false,
listeners: {
locationupdate: function(geo) {
alert('New latitude: ' + geo.getLatitude());
},
locationerror: function(geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) {
if(bTimeout){
alert('Timeout occurred.');
} else {
alert('Error occurred.');
}
}
}
});
geo.updateLocation();
试试这个。。
function getNameOfLocation(lat, lng) {
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
'latLng': latlng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
alert(results[0].formatted_address) // your result
//country name
for (var i=0; i<results[0].address_components.length; i++) {
for (var b=0;b<results[0].address_components[i].types.length;b++) {
if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
city= results[0].address_components[i];
break;
}
}
}
} else {
alert("No result");
}
} else {
alert("failed: " + status);
}
});
}
您可以向google地图API发送Ajax请求。例如:
http://maps.googleapis.com/maps/api/geocode/json?latlng=48.1471904942317,11.591434478759765&传感器=错误
然后解析JSON结果。