微信公众号内获取当前位置 省市区及坐标 坐标转换 百度地图(百度

2023-03-15  本文已影响0人  轩轩小王子
获取当前坐标
// 由于wx.getLocation内this指向已经发生改变,所以此处做一下转换
const that = this;
wx.getLocation({
    type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
    success: res => {
        // 由于项目中用到了百度地图,又需要向后台传gps坐标,所以此处做了一下转换 gcj02tobd09、gcj02towgs84是公司内部封装的一些转换方法
        that.position = gcj02tobd09(res.longitude, res.latitude);
        that.gpsInfo = gcj02towgs84(res.longitude, res.latitude);
        //图标Icon对象
        const icon = new BMap.Icon(normalVemImg, new BMap.Size(28, 34));
        // 在百度地图上显示坐标点
        that.mapPoint = new BMap.Point(that.position.longitude, that.position.latitude);
      // 设置中心点
        that.map.centerAndZoom(that.mapPoint, 18);
        const mapMarker = new BMap.Marker(that.mapPoint, {
            icon: icon
        });
        //将标注加到地图上
        that.map.addOverlay(mapMarker);
        that.map.disableDoubleClickZoom() // 禁止缩放
        that.map.disableScrollWheelZoom(); // 禁止缩放
        that.detailAddress();
    },
    fail: res => {
            console.error(res);
            that.seekFailed = true;
            uni.showToast({
                            title: '获取定位失败,请确保应用已授权并在手机设置中开启定位。',
                            icon: 'none',
                            duration: 3000
                            });
    }
});
获取当前位置(省市区)
detailAddress(){
    // gc.getLocation 方法内this指向已经变了
    let that=this;
    // 逆地址解析
    let gc = new BMap.Geocoder();
    gc.getLocation(that.mapPoint, function(rs){
        // 详细地址
        that.detailedAddress = rs.address;
        // 含省市区的对象(此处注意:市、区存在百度返回为空的情况)
        that.addressComponents = rs.addressComponents;
    })
}

码字不行,辛苦大佬,关注一下吧

image.png
上一篇 下一篇

猜你喜欢

热点阅读