在这里插入图片描述
uniapp官网文档

https://en.uniapp.dcloud.io/api/location/open-location.html

	<view class="map-content" @click.stop="kilometer(item)">
		<view class="km">
			{{item.distance||'0'}}km
		</view>
	</view>
	import map from '../../utils/map.js'
		onLoad() {
			let that = this
			let addressInfo = getApp().globalData.addressInfo;
			if (addressInfo) {
				that.addressInfo = addressInfo
				that.getOilList()
			} else {
			//这里是获取地理位置
				map.loadCity().then(res => {
					that.addressInfo = getApp().globalData.addressInfo
					that.getOilList()
				});
			}
		},
// 点击获取地图
	kilometer(e) {
		uni.openLocation({
			longitude: Number(e.lng),
			latitude: Number(e.lat),
			name: e.name,
			address: e.address
		})
	},

map.js页面对地理位置进行封装


import QQMapWX from '@/utils/qqmap-wx-jssdk.min.js'
var qqmapsdk = {};


// 获取位置授权
async function loadCity() {
    let that = this;
    return new Promise(function (resolve, reject) {
        uni.getSetting({
            success: (res) => {
                // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
                // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
                // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
                if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
                    uni.showModal({
                        title: '请求授权当前位置',
                        content: '需要获取您的地理位置,请确认授权',
                        success: function (res) {
							
                            if (res.cancel) {
                                uni.showToast({
                                    title: '拒绝授权',
                                    icon: 'none',
                                    duration: 1000
                                })
                                reject(false);
                            } else if (res.confirm) {
                                uni.openSetting({
                                    success: function (dataAu) {
                                        if (dataAu.authSetting["scope.userLocation"] == true) {
                                            uni.showToast({
                                                title: '授权成功',
                                                icon: 'success',
                                                duration: 1000
                                            })
                                            that.getLocation().then(function (res) {
                                                if (res) {
                                                    resolve(true);
                                                } else {
                                                    reject(false);
                                                }

                                            });
                                        } else {
                                            uni.showToast({
                                                title: '授权失败',
                                                icon: 'none',
                                                duration: 1000
                                            })
                                            reject(false);
                                        }
                                    }
                                })
                            }
                        }
                    })
                } else if (res.authSetting['scope.userLocation'] == undefined) {
                    that.getLocation().then(function (res) {
                        if (res) {
                            resolve(true);
                        } else {
                            reject(false);
                        }

                    });
                } else {
                    that.getLocation().then(function (res) {
                        if (res) {
                            resolve(true);
                        } else {
                            reject(false);
                        }

                    });

                }
            }
        })
    }).catch((e) => {})
}
//坐标获取城市
function getLocation() {
    let vm = this;
    return new Promise(function (resolve, reject) {
        uni.getLocation({
            type: 'wgs84',
            success: function (res) {
                getApp().globalData.latitude = res.latitude;
                getApp().globalData.longitude = res.longitude;
                uni.setStorageSync("longitude", res.longitude)
                uni.setStorageSync("latitude", res.latitude)
                vm.getLocal().then(function (res) {
                    if (res) {
                        resolve(true);
                    } else {
                        reject(false);
                    }
                });
            },
            fail: function (res) {
                reject(false);
            }
        })
    }).catch((e) => {})
}

// 坐标转换地址
function getLocal() {
    let vm = this;
    return new Promise(function (resolve, reject) {
        qqmapsdk = new QQMapWX ({
            key: 'asdfghjklqwertyuiop' //这里自己的key秘钥进行填充
        });
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: getApp().globalData.latitude,
                longitude: getApp().globalData.longitude
            },
            success: function (res) {
                getApp().globalData.addressInfo = res.result.address_component;
                resolve(true);
            },
            fail: function (res) {
                reject(false);
            }
        });
    }).catch((e) => {})
}

function calculateDistance(latitude, longitude) {
    let vm = this;
    return new Promise(function (resolve, reject) {
        qqmapsdk = new QQMapWX ({
            key: 'asdfghjklqwertyuiop' //这里自己的key秘钥进行填充
        });
        qqmapsdk.calculateDistance({
            to: [{
                latitude: latitude, //商家的纬度
                longitude: longitude, //商家的经度
            }],
            success: function (res) {
                resolve(res);
            },
            fail: function (res) {
                reject(res);
            }
        });
    }).catch((e) => {})
}

function selectLocation() {
    let that = this;
    return new Promise(function (resolve, reject) {
        uni.getSetting({
            success(res) {
                // 只返回用户请求过的授权
                let auth = res.authSetting;
                if (auth['scope.userLocation']) {
                    // 已授权,申请定位地址
                    resolve(true)
                } else if (auth['scope.userLocation'] === undefined) {
                    // 用户没有请求过的授权,不需要我们主动弹窗,微信会提供弹窗
                    resolve(true)
                } else if (!auth['scope.userLocation']) {
                    // 没有授权过,需要用户重新授权
                    // 这个弹窗是为了实现点击,不然openSetting会失败
                    uni.showModal({
                        title: '是否授权当前位置?',
                        content: '需要获取您的地理位置,请确认授权,否则定位功能将无法使用',
                        success: res => {
                            if (res.confirm) {
                                uni.openSetting({
                                    success(res) {
                                        let setting = res.authSetting;
                                        if (!setting['scope.userLocation']) {
                                            uni.showToast({
                                                title: '地址授权失败,定位功能无法使用',
                                                icon: 'none',
                                            });
                                            reject(false)
                                        } else {
                                            // 地址授权成功,申请定位地址
                                            resolve(true)
                                        }
                                    },
                                    fail(err) {
                                        // 需要点击,有时候没有点击,是无法触发openSetting
                                        console.log('open-setting-fail', err);
                                        reject(false)
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    }).catch((e) => {})
}

module.exports = {
    loadCity,
    getLocation,
    getLocal,
    getLocation,
    selectLocation,
    calculateDistance
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部