uniapp-小程序开发如何获取当前地理位置

小程序开发过程中获取用户当前地理位置,所以考虑基于用户当前经纬度,从而判断具体地理位置,具体代码如下:

fnGetlocation() {
			  let that = this;
			  uni.getLocation({
				type: "wgs84", //默认为 wgs84 返回 gps 坐标
				geocode: "true",
				isHighAccuracy: true,
				// accuracy: "best", // 精度值为20m
				success: function (res) {
					console.log("定位获取:", res);
					const re = that.wgs84Togcj02(res.longitude,res.latitude)
					console.log("当前平台: ",platform)
				  //that.getAreaCode(res.latitude, res.longitude);
				  if (platform == "mp-toutiao") {
					  console.log(res.city)
					  that.locationCityArea=res.city;
				  }else{
					  console.log(platform)
					  that.getAreaCode(re[1], re[0]);
				  }
				},
				fail(err) {
				  if (
					err.errMsg ===
					"getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化"
				  ) {
					uni.showToast({
					  title: "请勿频繁定位",
					  icon: "none",
					});
				  }
				  if (err.errMsg === "getLocation:fail auth deny") {
					// 未授权
					uni.showToast({
					  title: "无法定位,请重新获取位置信息",
					  icon: "none",
					});
					authDenyCb && authDenyCb();
					that.isLocated = false;
				  }
				  if (
					err.errMsg ===
					"getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF"
				  ) {
					uni.showModal({
					  content: "请开启手机定位服务",
					  showCancel: false,
					});
				  }
				  console.log(err.errMsg)
				},
			});
		},
		//下面是基于QQ sdk基于经纬度获取当前位置信息
		getAreaCode(latitude, longitude) {
			let that=this;
		  console.log("get area info from lat "+latitude+", long "+longitude)
		  //this.openMap(longitude,latitude)
		  // this.latitude=latitude.toFixed(6)
		  // this.longitude=longitude.toFixed(6)
		  
		  // 解析地址
		  QQMapWX.reverseGeocoder({
			  location: {
				  latitude: latitude,
				  longitude: longitude
			  },
			  success: function(res) {
				  console.log("解析地址成功"+ res);
				  // 省
				  let province = res.result.ad_info.province;
				  // 市
				  let city = res.result.ad_info.city;
				  let time = new Date()
				  //that.chengshi=city;
				  // console.log(province);
				  // console.log(city);
				  
				  that.locationCityArea=city;
				  //console.log(this.locationCityArea)
				  //将位置写入storage
				  //that.setLocation(latitude.toFixed(6),longitude.toFixed(6),city,res.result.address,time.toLocaleString())
			  },
			  fail: function(res) {
				  uni.showToast({
					  title: '定位失败',
					  duration: 2000,
					  icon: "none"
				  })
			  },
			  complete: function(res) {
				  console.log(res);
			  }
		  })
		 
		},
		
		wgs84Togcj02(lng, lat) {
			if (this.out_of_china(lng, lat)) {
				return [lng, lat]
			}
			//定义一些常量
			//GCJ02 转换为 WGS84
			var that = this;
			const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
			const PI = 3.1415926535897932384626;
			const a = 6378245.0;
			const ee = 0.00669342162296594323;
			let dlat = that.transformlat(lng - 105.0, lat - 35.0);
			let dlng = that.transformlng(lng - 105.0, lat - 35.0);
			let radlat = lat / 180.0 * PI;
			let magic = Math.sin(radlat);
			magic = 1 - ee * magic * magic;
			let sqrtmagic = Math.sqrt(magic);
			dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
			dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
			var mglat = lat + dlat;
			var mglng = lng + dlng;
			return [mglng, mglat]
			//return [lng * 2 - mglng, lat * 2 - mglat]
			},
			
			out_of_china(lng, lat) {
			return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
			},
			
			transformlat(lng, lat) {
			const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
			const PI = 3.1415926535897932384626;
			const a = 6378245.0;
			const ee = 0.00669342162296594323;
			let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(
			lng));
			ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
			ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
			ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
			return ret
			},
			transformlng(lng, lat) {
			const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
			const PI = 3.1415926535897932384626;
			const a = 6378245.0;
			const ee = 0.00669342162296594323;
			let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
			ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
			ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
			ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
			return ret
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

robin5911

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值