小程序火星坐标系 (GCJ-02) 转百度坐标系 (BD-09)和经纬度转度分秒格式

酸狗先带大家看看效果:
在这里插入图片描述
在这里插入图片描述
地图咋实现的就不写了小程序的map的API写的很清楚,主要看下转百度经纬度,想是uniapp搭建的,为啥用uniapp,以为不想用小程序开发工具~

获取下经纬度然后把经纬度存起来:

//  获取用户的地理位置,
			getLocation() {
				const that = this
				uni.getLocation({
					type: 'gcj02',
					altitude: true,
					success(res) {
						console.log(res)
						// console.log('当前位置的经度:' + res.longitude);
						// console.log('当前位置的纬度:' + res.latitude);
						that.latitude = res.latitude;
						that.longitude = res.longitude;
						that.nearby_search();
						// that.getsLocation(that.longitude, that.latitude);
						//判断是否开启经纬度转度分秒wechat=1为度分秒
						if (that.wechat == 1) {
							that.formatDegree(that.latitude);//纬度转度分秒
							// console.log(that.formatDegree(that.latitude));
							that.latitude = that.formatDegree(that.latitude);
							that.formatDegree(that.longitude);//经度转度分秒
							that.longitude = that.formatDegree(that.longitude);
						}
						var lng = res.longitude;//经度
						var lat = res.latitude;//纬度
						that.gcj2bdString(lng, lat);//GCJ-02纬度度调用方法转换成BD-09
						that.baidulongitude = (that.gcj2bdString(lng, lat).lng).toFixed(5);//经度保留小数点后5位
						that.baidulatitude = (that.gcj2bdString(lng, lat).lat).toFixed(5);//纬度保留小数点后5位
						//判断是否开启经纬度转度分秒
						if (that.wechat == 1) {
							that.formatDegree(that.baidulongitude);//百度纬度转度分秒
							that.baidulongitude = that.formatDegree(that.baidulongitude);
							that.formatDegree(that.baidulatitude);//百度经度转度分秒
							that.baidulatitude = that.formatDegree(that.baidulatitude);
						}
						// console.log(that.latitude);
					}
				})
			},

火星坐标系 (GCJ-02) 转百度坐标系 (BD-09):

因为除了百度地图,不管是高德地图、腾讯地图、谷歌地图用的都是火星坐标系 (GCJ-02),所以就需要百度转换下,我也是服了!

			// 地图转换GCJ-02坐标转换成百度的BD-09坐标
			// 参数形式为lng,lat
			// 返回值:lng,lat
			gcj2bdString(lng, lat) {
				const xpi = 3.14159265358979324 * 3000.0 / 180.0
				const z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * xpi);
				const theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * xpi);
				// console.log( z * Math.cos(theta) + 0.0065);
				return {
					lng: z * Math.cos(theta) + 0.0065,
					lat: z * Math.sin(theta) + 0.006
				}
			},

最后是经纬度转度分秒:

//经纬度转度分秒
			formatDegree(value) {
				if (value != null && value != '') {
					///<summary>将度转换成为度分秒</summary>  
					value = Math.abs(value); //返回数的绝对值
					var v1 = Math.floor(value); //度   //对数进行下舍入
					var v2 = Math.floor((value - v1) * 60); //分  
					var v3 = Math.round((value - v1) * 3600 % 60); //秒  //把数四舍五入为最接近的整数
					return v1 + "°" + v2 + "′" + v3 + "″";
				} else {
					return '' + "°" + '' + "′" + '' + "″";
				}
			},

希望对各位童鞋有帮助哈~
请添加图片描述

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
package com.map; public class GPSConverterUtils { public static final String BAIDU_LBS_TYPE = "bd09ll"; public static double pi = 3.1415926535897932384626; public static double a = 6378245.0; public static double ee = 0.00669342162296594323; /** * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System * 天地图 火星 * @param lat * @param lon */ public static GPS gps84_To_Gcj02(double lat, double lon) { if (outOfChina(lat, lon)) { return null; } double dLat = transformLat(lon - 105.0, lat - 35.0); double dLon = transformLon(lon - 105.0, lat - 35.0); double radLat = lat / 180.0 * pi; double magic = Math.sin(radLat); magic = 1 - ee * magic * magic; double sqrtMagic = Math.sqrt(magic); dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi); double mgLat = lat + dLat; double mgLon = lon + dLon; return new GPS(mgLat, mgLon); } /** * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return *火星地图 */ public static GPS gcj_To_Gps84(double lat, double lon) { GPS gps = transform(lat, lon); double lontitude = lon * 2 - gps.getLon(); double latitude = lat * 2 - gps.getLat(); return new GPS(latitude, lontitude); } /** * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的换算法 将 GCJ-02 坐标换成 BD-09 坐标 *火星百度 * @param gg_lat * @param gg_lon */ public static GPS gcj02_To_Bd09(double gg_lat, double gg_lon) { double x = gg_lon, y = gg_lat; double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi); double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi); double bd_lon = z * Math.cos(theta) + 0.0065; double bd_lat = z * Math.sin(theta) + 0.006;
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

柠檬树上柠檬果柠檬树下你和我

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

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

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

打赏作者

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

抵扣说明:

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

余额充值