uniapp-获取地理位置

254 阅读1分钟

地理位置

type:授权失败的时候是否弹框提示授权。 使用:

import { getLocation } from '@/utils/authUtils';

let res = await getLocation(0 ,this)
console.log(888888888 , res);

代码:

/**----------------------------------------------获取地理位置--------------------------------------- */
const locationErrhandler = (vm , resolve , reject) => {
    uni.showModal({
        content: "需要授权位置信息",
        confirmText: "确认授权",
    }).then(res =>{
        // 确认授权
        if (res[1]["confirm"]) {
            uni.openSetting({
                success:confirmRes => {
                    if (confirmRes.authSetting["scope.userLocation"]) {
                        // 授权成功
                        uni.showToast({
                            title: "授权成功",
                            icon: "none",
                        });
                        fnGetlocation(vm , location => {
                            resolve(location)
                        });
                    }else{
                        uni.showToast({
                            title: "授权失败,请重新授权",
                            icon: "none",
                        });
                        uni.showModal({
                            title: "授权",
                            content: "获取授权" + "失败,是否前往授权设置?",
                            success: result => {
                                if (result.confirm) {
                                    uni.openSetting();
                                }
                                reject()
                            },
                            fail:() =>{
                                uni.showToast({
                                    title: "系统错误!",
                                    icon: "none",
                                })
                                reject()
                            }
                        })
                    }
                }
            })
        }
        // 取消授权
        if (res[1]["cancel"]) {
          uni.showToast({
            title: "你拒绝了授权,无法获得周边信息",
            icon: "none",
          });
          reject()
        }
    })
}
const fnGetlocation =  (vm, callback) => {
    uni.getLocation({
        type: 'wgs84',
        geocode: "true",
        isHighAccuracy: "true",
        accuracy: "best", // 精度值为20m
        async success(res) {
            var location = {};
            location.lat = res.latitude;
            location.lon = res.longitude; // 根据经纬度获取城市和省份信息
            try {
                
                const data = {
                    latitude: res.latitude,
                    longitude: res.longitude
                }
                const resData = await vm.$http.post('xxxxxxxxxx' , data , {
                    header: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                })
                if (resData) {
                    resData.city_name = resData.cityName;
                    vm.$store.commit('SET_LOCATION' , Object.assign(location, resData))
                    callback(vm.$store.state.baselModule.location);
                }
            } catch (error) {
                callback(false);
            }
        },

        fail() {
            callback(false);
        }
    });
}
// type: 1手动触发,会指示开启定位。0不指示
export const getLocation = (type = 0 , vm) => {
    return new Promise((resolve, reject) => {
        uni.getSystemInfo({
            success(res) {
                let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
                let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
                if (locationEnabled == false || locationAuthorized == false) {
                    uni.showToast({
                        title: "请打开手机GPS",
                        icon: "none",
                    });
                    reject()
                } else {
                    uni.authorize({
                        scope: "scope.userLocation", //授权的类型
                        success: (res) => {
                            fnGetlocation(vm, location => {
                                resolve(location)
                            });
                        },
                        fail(err) {
                            if(type) locationErrhandler(vm, resolve, reject)
                            
                        }
                    })
                }
            }
        })
    })
}