用navigator.geolocation.watchPosition获取地理位置,在ie9以上支持这个方法,ie8及以下不行,在火狐和谷歌虽然支持了这个方法,但迟迟获取不了地理位置

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #text{width: 500px; height: 400px;}
    </style>
</head>
<body>
<input type="button" value="单次地理位置定位" id="btn" /><br>
<textarea id="text"></textarea>
<script type="text/javascript">
    window.onload = function(){
        var oBtn = document.getElementById('btn');
        var oText = document.getElementById('text');
        /*navigator.geolocation
         单次定位请求  :getCurrentPosition(请求成功,请求失败,数据收集方式)
         请求成功函数
         经度 :  coords.longitude
         纬度 :  coords.latitude
         准确度 :  coords.accuracy
         海拔 :  coords.altitude
         海拔准确度 :  coords.altitudeAcuracy
         行进方向 :  coords.heading
         地面速度 :  coords.speed
         时间戳 : new Date(position.timestamp)
         */
        //保存多次地理位置定位的内容
        var timer = null;
        oBtn.onclick = function(){
            alert('开始定位……');
            //watchPosition 相当于  setInterval
            timer = navigator.geolocation.watchPosition(function(position){//成功
                alert('定位成功!');
                oText.value += "经度:"+position.coords.longitude+'\n';
                oText.value += "纬度:"+position.coords.latitude+'\n';
                oText.value += "准确度:"+position.coords.accuracy+'\n';
                oText.value += "时间戳:"+new Date(position.timestamp)+'\n';
            },function(error){
                alert(error.code);
                //清除多次地理位置定位
                navigator.geolocation.clearWatch(timer);
            },{
                /*数据收集 :  json的形式
                 enableHighAcuracy  :  更精确的查找,默认false
                 timeout  :指定获取地理位置的超时时间,默认不限时,单位为毫秒
                 maximumAge :最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。
                 */
                enableHighAccuracy:true,
                maximumAge:2000
            });
        }
    }
</script>
</body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值