html获取当前经纬度

来源:互联网 发布:unity3d 麻将桌 编辑:程序博客网 时间:2024/06/06 05:51
<script>
var lng=0, lat=0;//经纬度
if (window.navigator.geolocation) {
    var options = {
        enableHighAccuracy: true,
    };
    window.navigator.geolocation.getCurrentPosition(handleSuccess, handleError, options);
} else {
//浏览器不支持html5来获取地理位置信息
}


function handleSuccess(position){
    // 获取到当前位置经纬度  本例中是chrome浏览器取到的是google地图中的经纬度
    lng = position.coords.longitude;
    lat = position.coords.latitude;
console.log('lng:'+lng+'========lat:'+lat);
}


function handleError(error){//地理位置信息获取失败
console.log('地理位置信息获取失败');
}
</script>
原创粉丝点击