1.写法一
getMapLocation(map){
// 定位对象方案1 : 百度获取经纬度
var geoc = new BMap.Geocoder();
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
console.log("当前位置经度为:"+r.point.lng+"纬度为:"+r.point.lat);
this.setLocation(r.point);
} else {
console.log('无法定位到您的当前位置,导航失败,请手动输入您的当前位置!'+this.getStatus());
}
},
{
//指示浏览器获取高精度的位置,默认为false
enableHighAccuracy: true
});
},
//获取地理位置的函数
setLocation(point){
geoc.getLocation(point, function(rs){
var addComp = rs.addressComponents;
var result = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
alert("当前的位置为:"+result);
});
},
这样写 位置 还是不准确
2.Geolocation用来定位用户的位置
因为这个可能和个人隐私相关,除非用户允许否则不能使用
IE9,Firefox,Chrome,Opera和Safari 5都支持这个特性。
注意:如果使用带有GPS的设备,例如iphone,Geolocation将会更加准确。
// 定位对象方案2:geolocation获取经纬度
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(pos) {
<!-- pos 的出参 -->
<!-- {speed: "-1.000000", longitude: "121.451945", latitude: "31.184739", accuracy: "65.000000", timestamp: "2018-06-27 07:12:33 +0000", …} -->
var point = new BMap.Point(pos.coords.longitude, pos.coords.latitude);
setLocation(point);
}, function(err) {
console.log(err,'err----')
})
}
测试 手机不支持 这种定位
![](https://img.haomeiwen.com/i3487404/0331bf1be8a1cb5d.png)
https://blog.csdn.net/for12/article/details/52803787
![](https://img.haomeiwen.com/i3487404/5ed51ce35e199adb.png)
分析的 [phonegap获取地理位置错误“User denied Geolocation”]
https://segmentfault.com/q/1010000000200734
![](https://img.haomeiwen.com/i3487404/69d39737d79ecf73.png)
写在最后
最后也实现了 效果 app原生获取gps定位 坐标后 传给 h5 再转为 百度坐标
再获取城市信息
这样定位比较准确
采用浏览器定位 也就是 js百度定位 只能定位到 大概的城市 不准
这就是 浏览器定位 也是 js百度定位 这个可以获取坐标 里面有城市信息 有乱码 可以再通过 城市信息api 获取城市信息 就没有乱码了
var geolocation = new BMap.Geolocation();
网友评论