uniapp定位

/**在app中获取定位*/

import permision from "@/utils/permission.js";
import userAuthorize from "@/utils/authorize.js"

const	authKey =	{
	text: '未开启位置权限',
	ios: 'location',
	Android: 'android.permission.ACCESS_FINE_LOCATION'
}

function getLocationClass(){
	return new Promise((resolve, reject) => {
		//api获取经纬度
		uni.getLocation({
		  type: 'wgs84',
		  isHighAccuracy: true,
		  success: (res) => {
		    //获取用户坐标
		    let latitude = res.latitude;
		    let longitude = res.longitude;
				resolve({latitude, longitude});
			},fail: (e) => {
				userAuthorize(authKey, state => {
					if(!state) return
					uni.showModal({
						title: '系统提示',
						content: '定位失败,请检查网络或系统定位',
						cancelText: '手动定位',
						confirmText: '重新定位',
						success: function (res) {
							if (res.confirm) {
								//重新调用定位方法
								getLocationClass();
							} else if (res.cancel) {
								resolve(false);
							}
						}
					});
				})
			}
		})
	})

}

/**判断当前手机是否开启了定位服务*/
function getLocation(){
	return new Promise(async (resolve, reject) => {
		if(permision.checkSystemEnableLocation()){
			//当前已经开启了定位服务,那么可以直接获取当前经纬度
			try{
				const data = await getLocationClass();
				resolve(data)
			}catch(e){
				reject(e)
			}
		}else{
			uni.showModal({
				title: '系统提示',
				content: '是否去开启定位',
				success: function (res) {
					if(uni.getSystemInfoSync().platform == 'ios'){   //ios系统的定位服务设置调起
						let UIApplication = plus.ios.import("UIApplication");
						let application2 = UIApplication.sharedApplication();
						let NSURL2 = plus.ios.import("NSURL");
						let setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
						application2.openURL(setting2);
						plus.ios.deleteObject(setting2);
						plus.ios.deleteObject(NSURL2);
						plus.ios.deleteObject(application2);
					}else{  //安卓系统的定位服务设置调起
						let Intent = plus.android.importClass('android.content.Intent');
						let Settings = plus.android.importClass('android.provider.Settings');
						let intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
						let main = plus.android.runtimeMainActivity();
						main.startActivity(intent); // 打开系统设置GPS服务页面
					}
				}
			})
		}
	})
}

export default getLocation

authorize.js和permision.js 看另外一篇文章uniapp授权_HSL-的博客-CSDN博客

wgs84获取到的经纬度偏差比较大,建议用gcj02 并且在app模块配置高德sdk

引入使用:

<template>
	<view>
		<u-button type="primary" text="定位" @click="hanldeLocation"></u-button>
		<view>{{ address }}</view>
	</view>
</template>

<script>
	import getLocationAppApi from '@/utils/location.js';
	export default {
		data() {
			return {
				address: ''
			}
		},
		methods: {
			getloadCity(latitude, longitude) {
				return new Promise((resolve, reject) => {
				  uni.showLoading({
					title: '获取中'
				  })
                    // 随便拿了一个腾讯地图的api获取地址,key换成自己的
				  wx.request({
				    url: 'https://apis.map.qq.com/ws/geocoder/v1/?location='+latitude+','+longitude+'&key=xxxxxxxxx',
				    success: function(res) {
					  uni.hideLoading()
				      resolve(res);
				    },fail: function(err) {
					  uni.hideLoading()
				      reject(err)
				    }
				  })
				})
			},
			
            
			async hanldeLocation() {
				try{
                    // 授权获取经纬度
					const {latitude, longitude} = await getLocationAppApi();
                    // 获取地址
					const res = await this.getloadCity(latitude, longitude);
					this.address = res.data.result.address
				}catch(e){
					console.log(e);
				}
			}
		}
	}
</script>

UniApp是一款基于Vue.js开发的跨平台应用框架,可以快速高效地开发小程序、H5、App等多端应用。虽然UniApp自带了一些基本的API用于获取当前位置信息,但是如果需要更精确的定位或者需要一些其他功能,可以选择使用定位插件来扩展UniApp定位能力。 UniApp定位插件有很多种,比如uni-location、uni-geolocation等。这些插件可以实现获取当前位置坐标、获取地理位置信息、监听位置变化等功能。 使用UniApp定位插件,首先需要在项目中安装所需的插件。可以通过在App.vue中引入所需插件的方式来注册插件,并在页面中进行使用。 在代码中,通过调用插件暴露出来的API方法,可以获取当前位置的经纬度信息。可以通过这些信息来实现一些定位相关的功能,比如根据经纬度查询当前位置的天气、附近的商家等。 定位插件还提供了一些其他功能,比如监听位置变化的方法,可以实时获取位置信息的更新。这对于一些需要实时获取位置的应用来说十分重要。另外,插件还可以设置定位的精确度、定位超时时间等参数,以满足不同需求。 总的来说,UniApp定位插件可以帮助我们在开发过程中更方便地获取和处理位置信息,扩展了UniApp定位能力,提高了应用的实用性和用户体验。当然,开发者还可以根据具体需求选择其他的定位插件,以满足自己的开发需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值