/*
 2021/8/26
 xh
 获取地理位置经纬度及城市名(h5,app,微信小程序三端)
 */
// #ifdef H5
//引入jquery文件,用jquery的jsonp来访问腾讯地图的逆地址解析,只适用于h5和pc
import $ from './jquery-3.0.0.min.js'
// #endif
// #ifdef MP-WEIXIN
//腾讯地图的逆地址解析,下载地址:https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview
import QQMapWX  from './qqmap-wx-jssdk.js'
// #endif

//获取位置信息
export const getLocation = function(){
  return new Promise((rel,rej)=>{
		// #ifdef H5
		uni.getLocation({
			type:'gcj02',
			success:async (res)=>{
				/* console.log('当前位置的经度:' + res.longitude);
				console.log('当前位置的纬度:' + res.latitude); */
				var city = await getH5MapCity(res.latitude,res.longitude);
				let obj = {
					name:city,
					jing:res.longitude,
					wei:res.latitude
				}
				console.log('H5')
				if(city !== false){
					rel(obj);
				}else{
					rej('获取失败');
				}
			},
			fail(res){
				rej('获取失败');
				uni.showToast({
					title:'位置获取失败',
					icon:'none',
					duration:2000
				})
			}
		})
		/* // 腾讯地图Api
		const qqmapsdk = new QQMapWX({ key: "xxxxxxx" });
		uni.getLocation({
			type:'gcj02',
			success(res){
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: res.latitude,
						longitude: res.longitude
					},
					success(e) {
						let obj = {
							name:e.result.ad_info.city,
							jing:res.longitude,
							wei:res.latitude
						}
						if(e.result.ad_info.city){
							rel(obj);
						}else{
							rej('获取失败');
						}
						console.log(e);
					},
					fail(res){
						rej('获取失败');
						uni.showToast({
							title:'位置获取失败',
							icon:'none',
							duration:2000
						})
					}
				})
			}
		}) */
		// #endif
		
		//app位置获取
		// #ifdef APP-PLUS
		uni.getLocation({
			type:'gcj02',
			geocode:true,
			success(res){
				console.log(res)
				let obj = {
					name:res.address.city,
					jing:res.longitude,
					wei:res.latitude
				}
				console.log('APP')
				if(res.address.city){
					rel(obj);
				}else{
					rej('获取失败');
				}
			},
			fail(res){
				rej('获取失败');
				uni.showToast({
					title:'位置获取失败',
					icon:'none',
					duration:2000
				})
			}
		})
		// #endif
		
		//微信小程序
		// #ifdef MP-WEIXIN
		// 获取授权信息
		uni.getSetting({
		  success: res => {
			if (res.authSetting && res.authSetting.hasOwnProperty("scope.userLocation")) {
			  if (res.authSetting["scope.userLocation"]) {
				getCityInfo();
			  } else {
				uni.showModal({
				  title: "提示",
				  content: "请重新授权获取你的地理位置,否则部分功能将无法使用",
				  success: (res) => {
					if (res.confirm) {
					  uni.openSetting({
						success: () => getCityInfo()
					  });
					} else {
					  rej('获取失败');
					  uni.showToast({
						title:'请授权获取你的地理位置,否则部分功能将无法使用!',
						icon:'none',
						duration:2000
					  })
					}
				  },
				  fail(res){
					rej('获取失败');
					uni.showToast({
						title:'位置获取失败',
						icon:'none',
						duration:2000
					})
				  }
				});
			  }
			} else {
			  getCityInfo();
			}
		  }
		})
	 
		// 获取地理位置信息
		const getCityInfo = () => {
			  // 腾讯地图Api
			  const qqmapsdk = new QQMapWX({ key: "xxxxxxxxxxxx" });
			  // 授权
			  uni.authorize({
				scope: "scope.userLocation",
				success: () => {
				  uni.getLocation({
					type: "gcj02", //  wgs84: 返回GPS坐标,gcj02: 返回国测局坐标
					success: res => {
					  qqmapsdk.reverseGeocoder({
						location: {
							latitude: res.latitude,
							longitude: res.longitude
						},
						success(e) {
							let obj = {
								name:e.result.ad_info.city,
								jing:res.longitude,
								wei:res.latitude
							}
							if(e.result.ad_info.city){
								rel(obj);
							}else{
								rej('获取失败');
								uni.showToast({
									title:'位置获取失败',
									icon:'none',
									duration:2000
								})
							}
							console.log(e);
						},
						fail(res){
							rej('获取失败');
							uni.showToast({
								title:'位置获取失败',
								icon:'none',
								duration:2000
							})
						}
					  });
					}
				  });
				},
				fail(res){
					rej('获取失败');
					uni.showToast({
						title:'请授权获取你的位置,否则部分功能将无法使用!',
						icon:'none',
						duration:2000
					})
				}
			})
		}
		// #endif
	})
}

// h5腾讯地图逆解析(将经纬度转成城市名)
function getH5MapCity(latitude, longitude){
	return new Promise((rel,rej)=>{
		var url = 'https://apis.map.qq.com/ws/geocoder/v1/?';//腾讯地图经纬度转城市名的链接
		var data ={
					key:'xxxxxxxxx',//腾讯地图密钥
					output:'jsonp',
					location:latitude+','+longitude //经纬度
				}
		//用jquery的jsonp来解决跨域问题
		$.ajax({
			type:'get',
			dataType:'jsonp',
			data,
			jsonp:"callback",
			url,
			success(res){
				var cityName = res.result.address_component.city;
				console.log(res);
				rel(cityName);
			},
			error(res){
				uni.showToast({
					title:'位置获取失败',
					icon:'none',
					duration:2000
				})
				rej(false);
			}
		})
	})
}



jsonp插件来解决跨域,实现逆解析

下载 vue-jsonp 插件

main.js

import {VueJsonp} from 'vue-jsonp'
Vue.use(VueJsonp);//用jsonp来解决腾讯地图逆解析跨域问题

页面

//腾讯地图逆解析,解决跨域
getUserLocation(e) {
	const that = this;
	let locationObj = e.latitude + ',' + e.longitude;
	let url = 'https://apis.map.qq.com/ws/geocoder/v1/?';
	this.$jsonp(url, {
		key:that.$key,//腾讯地图密钥
		output:'jsonp',
		location:e.latitude+','+e.longitude //经纬度
	})
	.then(res => {
		console.log(res)
	})
	.catch(err => {
		uni.showToast({
			title:'解析失败',
			icon:'none'
		})
	});

},
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐