微信上的网站 用HTML5获取地理位置

今天折腾了一天,因为项目的需要,需要在微信网站上得到当前的地理位置。采用的是HTML5开发,但是实验了很多种方法,都不行。HTML5获取地理位置信息,是通过ip获取,ip的位置和你本人的位置差距还是很大的,所以采用HTML5获取地位置很不准确。HTML5不能打开你本地的GPS,不像客户端一样,可以打开你的手机GPS进行定位,这样HTML5的地位就形同虚设。没有起到真正定位的作用。


下面是一段试验过的代码

<!DOCTYPE html>

<html>

<title>HTML5 调用百度地图API地理定位实例 </title>

    <head>
		<meta name="viewport" content="width=device-width,initial-scale=1" /> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>

    </head>

<body style='margin:50px 10px;'>

        <div id="status" style="text-align: center"></div>

        <div style="width:600px;height:480px;border:1px solid gray;margin:30px auto" id="container"></div>

</body>

</html>



<script type="text/javascript">

    window.onload = function() {

        if(navigator.geolocation) {

            document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser.";

            navigator.geolocation.getCurrentPosition(updateLocation);

        }

    };



    function updateLocation(position) {

        var latitude = position.coords.latitude;

        var longitude = position.coords.longitude;

        if(!latitude || !longitude) {

            document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser, but location is currently not available.";

            return;

        }



        var map = new BMap.Map("container");                     // 创建Map实例

        var point = new BMap.Point(longitude, latitude);        // 创建点坐标

        map.centerAndZoom(point, 15);                            // 初始化地图,设置中心点坐标和地图级别。

        var marker = new BMap.Marker(point);                        // 创建标注

        map.addOverlay(marker);                                    //将标注添加到地图中

    };



</script>

测试条件是打开了WIFI,但是很不准定位,相差一千米左右。如果不用WIFI,只用移动的2G  3G信号基站定位,是获取不到的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值