今天折腾了一天,因为项目的需要,需要在微信网站上得到当前的地理位置。采用的是HTML5开发,但是实验了很多种方法,都不行。HTML5获取地理位置信息,是通过ip获取,ip的位置和你本人的位置差距还是很大的,所以采用HTML5获取地位置很不准确。HTML5不能打开你本地的GPS,不像客户端一样,可以打开你的手机GPS进行定位,这样HTML5的地位就形同虚设。没有起到真正定位的作用。
下面是一段试验过的代码
HTML5 调用百度地图API地理定位实例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); //将标注添加到地图中
};
测试条件是打开了WIFI,但是很不准定位,相差一千米左右。如果不用WIFI,只用移动的2G 3G信号基站定位,是获取不到的。