百度地图定位
作者:互联网
在Vue2.0里,不要用 import BMap from 'BMap'
class Geolocation { constructor() { this.cityName = '' this.address = '' } /** * 浏览器定位 */ getGeolocation(callback) { try{ let self = this var map = new BMap.Map("allmap") var point = new BMap.Point(116.331398,39.897445) map.centerAndZoom(point,12) 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) Toast('您的位置:'+r.point.lng+','+ r.point.lat); var gc = new BMap.Geocoder(); let addressReal = gc.getLocation(r.point, (rs)=>{ var addComp = rs.addressComponents; var address = addComp.province + ' ' + addComp.city + ' ' + addComp.district; callback(address) return address }); } else { Toast('浏览器定位失败') self.getSDK() } }) } catch (e) { console.log(e) } } /** * 辅助百度SDK定位 */ getSDK() { var map = new BMap.Map("allmap") var point = new BMap.Point(116.331398,39.897445) map.centerAndZoom(point,12) var geolocation = new BMap.Geolocation() // 开启SDK辅助定位 geolocation.enableSDKLocation() geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point) map.addOverlay(mk) map.panTo(r.point) Toast('您的位置:'+r.point.lng+','+r.point.lat) } else { Toast('百度地图定位失败') } }); } /** * IP 定位 */ getIP() { try { let promise = new Promise((resolve, reject) => { var map = new BMap.Map("allmap") var point = new BMap.Point(116.331398,39.897445) map.centerAndZoom(point,12) function myFun(result){ var cityName = result.name map.setCenter(cityName) resolve(result) // Toast("当前定位城市:"+ cityName) } var myCity = new BMap.LocalCity() myCity.get(myFun) }) return promise } catch(e) { Toast('IP定位失败') return null } } } export default Geolocation
调用: let geolocation = new Geolocation(); geolocation.getGeolocation((res) => { console.log(res) //拿到地址 });
标签:定位,geolocation,point,地图,map,BMap,var,new,百度 来源: https://www.cnblogs.com/init00/p/12762477.html