admin 发表于 2024-4-2 12:48:20

uniapp开发小程序和app使用定位服务判断

if (uni.canIUse('getLocation')) {
                                        console.log('可以用')
                                       
                                        // #ifdef APP
                                        const appAuthorizeSetting = uni.getAppAuthorizeSetting();
                                        // console.log(appAuthorizeSetting.locationAuthorized);
                                        if (appAuthorizeSetting.locationAuthorized != 'authorized') {
                                                uni.showModal({
                                                        title: '提示',
                                                        content: '请开启手机位置服务并允许我们获取您的位置信息后重试',
                                                        showCancel: false,
                                                        success() {
                                                                uni.navigateBack();
                                                        }
                                                })
                                                return;
                                        }
                                        // #endif
                                       
                                        // #ifdef MP-WEIXIN
                                        uni.getSetting({
                                                success(res) {
                                                        console.log(res)
                                                        if (!res.authSetting['scope.userLocation']) {
                                                                uni.showModal({
                                                                        title: '提示',
                                                                        content: '位置获取失败,请在打开的设置页面在“位置消息”栏选择“使用小程序时允许”',
                                                                        showCancel: false,
                                                                        success() {
                                                                                uni.openSetting();
                                                                        }
                                                                })
                                                                return;
                                                        }
                                                }
                                        });
                                        // #endif
                                       
                                        uni.getLocation({
                                                success(res) {
                                                        console.log(res)
                                                        uni.setStorageSync('lat', res.latitude);
                                                        uni.setStorageSync('lng', res.longitude);
                                                        uni.navigateTo({
                                                                url: '/pages/user/userinfo'
                                                        })
                                                }
                                        })
                                       
                                } else {
                                        console.log('不可用')
                                }

页: [1]
查看完整版本: uniapp开发小程序和app使用定位服务判断