export default {
    getPosition() {
        uni.showLoading({
            title: "获取定位中...",
            mask: true,
        });
        return new Promise((resolve, reject) => {
            uni.getLocation({
                type: 'gcj02',
                isHighAccuracy: true,
                success: (res) => {
                    uni.hideLoading()
                    resolve(res)
                },
                fail: (e) => {
                    uni.hideLoading()
                    reject(e)
                    const { locationEnabled } = uni.getSystemSetting()//是否开启定位权限
                    if (!locationEnabled) {
                        uni.showModal({
                            title: '提示',
                            content: '获取定位失败,是否授权打开定位',
                            success: (res) => {
                                if (res.confirm) {
                                    uni.getSystemInfo({
                                        success: (sys) => {
                                            if (sys.platform == 'ios') {
                                                plus.runtime.openURL(
                                                    "app-settings://");
                                            } else {
                                                const main = plus.android
                                                    .runtimeMainActivity();
                                                const Intent = plus.android
                                                    .importClass(
                                                        "android.content.Intent"
                                                    );
                                                //可能应该直接进入应用列表的权限设置?=> android.settings.APPLICATION_SETTINGS
                                                const mIntent = new Intent(
                                                    'android.settings.LOCATION_SOURCE_SETTINGS'
                                                );
                                                main.startActivity(mIntent);
                                            }
                                        }
                                    })
                                }
                            }
                        })
                    } else {
                        uni.$u.toast('获取位置失败,请到空旷的地方试试!');
                    }

                },
            });
        })

    },


}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐