用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - html代码库

浏览器获取当前位置信息

2014-06-16 作者: 路易斯陈凯瑞举报

[html]代码库

<html>
    <head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<script language="javascript">
    var options = {
        enableHighAccuracy: true,
        maximumAge: 60000,
        timeout: 45000
    };
    window.onload = function() {
        if (window.navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(successCallback, errorCallback,options);
    } else {
            alert('Your browser does not natively support geolocation.');
    }
}
 
    function    successCallback(position){
        alert("成功获取地理位置...");
        var posiText =  "当前维度:"+position.coords.latitude;
        posiText += "</br>" +"当前经度:"+position.coords.longitude;
        posiText +="</br>" +"精确到:"+position.coords.accuracy +"米";
        if (position.coords.altitude)
            posiText += '</br>高度: ' + position.coords.altitude + " 米";
        posiText += '<br/>距离北京天安门' + distance(position.coords.latitude,position.coords.longitude,39.5427,116.2317) +'公里';
        document.getElementById("positionInfo").innerHTML = posiText;
    }
    function errorCallback(error){
         
    }
     
     
    function toRadians(degree) {
        return degree * Math.PI / 180;
}
  //通过经纬度计算距离的函数
    function distance(latitude1, longitude1, latitude2, longitude2) {
 
        // R is the radius of the earth in kilometers      
        var R = 6371;      
        var deltaLatitude = toRadians(latitude2-latitude1);        
        var deltaLongitude = toRadians(longitude2-longitude1);     
        latitude1 =toRadians(latitude1);       
        latitude2 =toRadians(latitude2);       
        var a = Math.sin(deltaLatitude/2) * Math.sin(deltaLatitude/2) +Math.cos(latitude1) *Math.cos(latitude2) * Math.sin(deltaLongitude/2) * Math.sin(deltaLongitude/2);     
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));      
        var d = R * c;     
        return d;
}
 
</script>
</head>
<body>
    当前位置信息<br/>
    <div id ="positionInfo"></div>
     
</body>
     
</html>

[代码运行效果截图]


浏览器获取当前位置信息


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...