Javascript 如何从经度和纬度获取当前位置名称?

Javascript 如何从经度和纬度获取当前位置名称?,javascript,html,google-maps,Javascript,Html,Google Maps,我使用这段代码通过google maps api获取用户的当前位置,以纬度和经度的形式获取位置,并以LatLng变量获取位置 但是在那之后,当我把纬度和经度转换成一个地址时,它就不起作用了 if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function (p){ var LatLng = new google.maps.LatLng(p.coords.latitude,p.coords.longitude

我使用这段代码通过google maps api获取用户的当前位置,以纬度和经度的形式获取位置,并以LatLng变量获取位置

但是在那之后,当我把纬度和经度转换成一个地址时,它就不起作用了

if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (p){
var LatLng = new google.maps.LatLng(p.coords.latitude,p.coords.longitude);
    alert(LatLng);
    alert(p.coords.latitude);
    alert(p.coords.longitude);
    var mapOptions = {
         center: LatLng,
         zoom: 13,
         mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'LatLng': LatLng }, function (results, status){
        alert(geocoder);   
        if (status == google.maps.GeocoderStatus.OK){
             if (results[1]){
                 alert("Location: " + results[1].formatted_address);
             }
        }
});

首先,正如评论员帕克所说,jsp和java标记与您的文章无关。我把它拿走了

你应该做反向地理编码。相反,将地图上的位置转换为人类可读地址的过程称为反向地理编码

有关详细信息,请参阅下面的谷歌地图url

请参阅以下将lat和lan映射到位置的片段

 var latlng = {lat: parseFloat(latlngStr[0]), lng: parseFloat(latlngStr[1])};
 geocoder.geocode({'location': latlng}, function(results, status) {
    if (status === 'OK') {
      if (results[0]) {
        map.setZoom(11);
        var marker = new google.maps.Marker({
         position: latlng,
         map: map
        });
        infowindow.setContent(results[0].formatted_address);
        infowindow.open(map, marker);
      } else {
        window.alert('No results found');
      }
    } else {
      window.alert('Geocoder failed due to: ' + status);
    }
如果有帮助,请告诉我。

 $(document).ready(function() {
     var currgeocoder;

     //Set geo location  of lat and long

     navigator.geolocation.getCurrentPosition(function(position, html5Error) {

         geo_loc = processGeolocationResult(position);
         currLatLong = geo_loc.split(",");
         initializeCurrent(currLatLong[0], currLatLong[1]);

    });

    //Get geo location result

   function processGeolocationResult(position) {
         html5Lat = position.coords.latitude; //Get latitude
         html5Lon = position.coords.longitude; //Get longitude
         html5TimeStamp = position.timestamp; //Get timestamp
         html5Accuracy = position.coords.accuracy; //Get accuracy in meters
         return (html5Lat).toFixed(8) + ", " + (html5Lon).toFixed(8);
   }

    //Check value is present or not & call google api function

    function initializeCurrent(latcurr, longcurr) {
         currgeocoder = new google.maps.Geocoder();
         console.log(latcurr + "-- ######## --" + longcurr);

         if (latcurr != '' && longcurr != '') {
             var myLatlng = new google.maps.LatLng(latcurr, longcurr);
             return getCurrentAddress(myLatlng);
         }
   }

    //Get current address

     function getCurrentAddress(location) {
          currgeocoder.geocode({
              'location': location

        }, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results[0]);
                $("#address").html(results[0].formatted_address);
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
     }
});

</script>
$(文档).ready(函数(){
可变电流地理编码器;
//设置横向和纵向的地理位置
navigator.geolocation.getCurrentPosition(函数(位置,HTML5错误){
地理位置=处理地理位置结果(位置);
currLatLong=地理位置拆分(“,”);
初始化current(currLatLong[0],currLatLong[1]);
});
//获取地理定位结果
函数处理地理位置结果(位置){
html5Lat=position.coords.latitude;//获取纬度
html5Lon=position.coords.longitude;//获取经度
html5TimeStamp=position.timestamp;//获取时间戳
html5accurity=position.coords.accurity;//以米为单位获取精度
return(html5Lat).toFixed(8)+“,”+(html5Lon).toFixed(8);
}
//检查值是否存在&调用googleapi函数
函数初始化电流(latcurr、longcurr){
currgeocoder=新的google.maps.Geocoder();
console.log(latcurr+”------------+longcurr);
如果(latcurr!=''&&longcurr!=''){
var mylatng=new google.maps.LatLng(latcurr,longcurr);
返回getCurrentAddress(myLatlng);
}
}
//获取当前地址
函数getCurrentAddress(位置){
currogeocoder.geocode({
“位置”:位置
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
console.log(结果[0]);
$(“#地址”).html(结果[0]。格式化的#地址);
}否则{
警报('地理编码因以下原因未成功:'+状态);
}
});
}
});

javascript!=javaRemove标签java、java和javascript是不同的