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

来源:互联网 发布:淘宝领阿里小号 编辑:程序博客网 时间:2024/04/29 05:18

今天折腾了一天,因为项目的需要,需要在微信网站上得到当前的地理位置。采用的是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信号基站定位,是获取不到的。

0 0
原创粉丝点击