javascript 如何使用纬度和经度获取位置名称或城市?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15662775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:35:42  来源:igfitidea点击:

How do I get location name or city using latitude & longitudes?

javascriptgeolocation

提问by user2216267

I'm using the below code, but how to get the city name or the location name from the longitude & latitude?

我正在使用以下代码,但如何从经度和纬度获取城市名称或位置名称?

var x=document.getElementById("location");
function getLocation()
{
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
    else {
        x.innerHTML="Geolocation is not supported by this browser.";
    }
}

function showPosition(position)
{
    x.innerHTML="Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude;  
}

采纳答案by RouteMapper

You can use the Google Maps v3 API to perform reverse geocoding on a latitude and longitude pair. You can find an example of how to do that here: https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding.

您可以使用 Google Maps v3 API 对纬度和经度对执行反向地理编码。您可以在此处找到如何执行此操作的示例:https: //developers.google.com/maps/documentation/geocoding/#ReverseGeocoding