坐标转高德地标(火星坐标/国测坐标)"/>
GPS、(百度)坐标转高德地标(火星坐标/国测坐标)
android\ios壳嵌入h5调取原生功能获取当前的地理位置
wgs84 返回 gps 坐标,gcj02 返回国测局坐标。
gcj02参数获取的地址有偏差,而用定位 wgs84参数则不返回地址,所以需要将wgs84坐标转gcj02坐标,然后再调用高德地图接口获取地址。
方法一:
1.GPS转高德坐标
class Gps {//构造函数constructor(obj = {}) {let { longitude, latitude } = obj;if (longitude === undefined || latitude === undefined) {return console.error("经纬度参数不能为空!");}this.PI = 3.14159265358979324;return this.getCoordinate(longitude, latitude);};//纬度转换transformLatitude(x, y) {let num = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));num += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;num += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;num += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;return num;};//经度转换transformLongitude(x, y) {let num = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));num += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;num += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;num += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;return num;};// 坐标转换calculation(longitude, latitude) {let a = 6378245.0; // 卫星椭球坐标投影到平面地图坐标系的投影因子。let ee = 0.00669342162296594323; // 椭球的偏心率。let lat = this.transformLatitude(longitude - 105.0, latitude - 35.0);let lng = this.transformLongitude(longitude - 105.0, latitude - 35.0);let radLat = latitude / 180.0 * this.PI;let magic = Math.sin(radLat);magic = 1 - ee * magic * magic;let sqrtMagic = Math.sqrt(magic);lat = (lat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);lng = (lng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);return {'longitude': lng,'latitude': lat,};};// 判断是否为国外坐标isOutOfChina(longitude, latitude) {if (longitude < 72.004 || longitude > 137.8347) {return true;};if (latitude < 0.8293 || latitude > 55.8271) {return true;};return false;};// GPS坐标 转 高德坐标getCoordinate(longitude, latitude) {longitude = Number(longitude);latitude = Number(latitude);if (this.isOutOfChina(longitude, latitude)) {//国外return {"longitude": longitude,"latitude": latitude,}} else {//国内let obj = this.calculation(longitude, latitude);return {"longitude": longitude + obj.longitude,"latitude": latitude + obj.latitude,};}};
}
//使用方法
var obj = new Gps({"latitude": 23.136245,"longitude": 113.271139,
});
console.log('obj: ', `${obj.longitude},${obj.latitude}`);
2.百度坐标转高德坐标
function bd_convert_gd(coordinate) {var bd_lng = coordinate[0];var bd_lat = coordinate[1];var pi = 3.14159265358979324 * 3000.0 / 180.0;var x = bd_lng - 0.0065;var y = bd_lat - 0.006;var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);var gd_lng = z * Math.cos(theta);var gd_lat = z * Math.sin(theta);return [gd_lng,gd_lat];
}
3.(高德转百度坐标)
function gd_convert_bd(coordinate){var pi = 3.14159265358979324 * 3000.0 / 180.0;var x = coordinate[0];var y = coordinate[1];var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);var bd_lng = z * Math.cos(theta) + 0.0065;var bd_lat = z * Math.sin(theta) + 0.006;return [bd_lng.toFixed(6),bd_lat.toFixed(6)];
}
方法二:【推荐】(可将不同的坐标系转高德坐标)
高德地图官网提供GPS坐标转换国测坐标的接口【开发 > Web服务 API > 开发指南 > API文档 > 坐标转换】:
文档:
API接口:=e5192572800ebee27ec1dd4de9de2b51&locations=113.271139,23.136245&coordsys=gps
高德地图【开发 > Web服务 API > 开发指南 > API文档 > 地理/逆地理编码】
文档:
API接口:=e5192572800ebee27ec1dd4de9de2b51&location=113.27647443065139,23.133575709591703
注意:高德地图官方坐标转地址API(get请求),key需要到官网申请。申请的必须是【 Web服务 】的key,而【 Web端(JS API) 】的key是不能调用上面的接口
更多推荐
GPS、(百度)坐标转高德地标(火星坐标/国测坐标)
发布评论