3.地理定位 Geolocation

//1.判断浏览器是否支持位置接口

function support_location(){
    if(navigator.geolocation) {
        // 支持
        console.log("支持地理位置接口");
        document.getElementById("dv1").innerHTML="支持地理位置接口";
    } else {
        // 不支持
        console.log("不支持地理位置接口");
        document.getElementById("dv1").innerHTML="不支持地理位置接口";
    }
}

//2.判断用户是否授权
ffunction shouquan_location(){
    var option = {
          // 指示浏览器获取高精度的位置,默认为false
          enableHighAccuracy: true,
  // 指定获取地理位置的超时时间,默认不限时,单位为毫秒
     timeout: 5000,
          // 最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。
          maximumAge: 0
    };

    navigator.geolocation.getCurrentPosition(successfun,errorfun,option);

}
//-----------------------
function successfun(event) {
    var lat = event.coords.latitude; //纬度
    var lag = event.coords.longitude; //经度
    alert('纬度:'+lat+',经度:'+lag);

    var latlon = event.coords.latitude+','+event.coords.longitude;
    var url = "http://api.map.baidu.com/geocoder/v2/?ak=C93b5178d7a8ebdb830b9b557abce78b&callback=renderReverse&location="+latlon+"&output=json&pois=0";
    $.ajax({
        type: "GET",
        dataType: "jsonp",
        url: url,
        beforeSend: function(){
            $("#baidu_geo").html('正在定位...');
        },
        success: function (json) {
            if(json.status==0){
                $("#baidu_geo").text(json.result.formatted_address);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $("#baidu_geo").text(latlon+"地址位置获取失败");
        }
    });
}
//--------------------------------
function errorfun(error)
{
    switch(error.code)
    {
        case error.PERMISSION_DENIED:
            document.getElementById("dv2").innerHTML="用户拒绝对获取地理位置的请求。"
            break;
        case error.POSITION_UNAVAILABLE:
            document.getElementById("dv2").innerHTML="位置信息是不可用的。"
            break;
        case error.TIMEOUT:
            document.getElementById("dv2").innerHTML="请求用户地理位置超时。"
            break;
        case error.UNKNOWN_ERROR:
            document.getElementById("dv2").innerHTML="未知错误。"
            break;
    }
}


--------------
getCurrentPosition()返回成功后,position对应的属性值
coords.latitude 十进制数的纬度
coords.longitude 十进制数的经度
coords.accuracy 位置精度
coords.altitude 海拔,海平面以上以米计
coords.altitudeAccuracy 位置的海拔精度
coords.heading 方向,从正北开始以度计
coords.speed 速度,以米/每秒计
timestamp 响应的日期/时间
-----------------------------
watchPosition
// watchPosition方法一样可以设置三个参数  
 // 使用方法和getCurrentPosition方法一致,只是执行效果不同。  
 // getCurrentPosition只执行一次  
 // watchPosition只要设备位置发生变化,就会执行  
     var watcher_id = navigator.geolocation.watchPosition(getSuccess, getError, getOptions);  
  //clearwatch用于终止watchPosition方法  
  navigator.geolocation.clearWatch(watcher_id);  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值