JavaScript- 获取经度纬度

  昨天获得一个小需求,需要取地理位置。

  通过以下的JS能获得经度和纬

if(navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(function (p) {
            var latitude = p.coords.latitude//纬度
            var longitude = p.coords.longitude;
            alert("ss");
            console.log(p)
          
        }, function (e) {//错误信息
            var aa = e.code + "
" + e.message;
            alert(aa);
        }
        );
    }

  

  然后将取到经度和纬度传入:http://api.map.baidu.com/geocoder?output=json&location=23.263221,116.663643

  获取返回过来的JSON,解析后就取得地点的值

原文地址:https://www.cnblogs.com/cxeye/p/4863778.html