百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换

javascript 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		
		<script>
			
		  /**
			 * 
			 * 百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
			 */
			
			//定义一些常量
			var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
			var PI = 3.1415926535897932384626;
			var a = 6378245.0;
			var ee = 0.00669342162296594323;
			
			/**
			 * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
			 * 即 百度 转 谷歌、高德
			 * @param bd_lon
			 * @param bd_lat
			 * @returns {*[]}
			 */
			function bd09togcj02(bd_lon, bd_lat) {
			    var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
			    var x = bd_lon - 0.0065;
			    var y = bd_lat - 0.006;
			    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
			    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
			    var gg_lng = z * Math.cos(theta);
			    var gg_lat = z * Math.sin(theta);
			    return [gg_lng, gg_lat]
			}
			
			/**
			 * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
			 * 即谷歌、高德 转 百度
			 * @param lng
			 * @param lat
			 * @returns {*[]}
			 */
			function gcj02tobd09(lng, lat) {
			    var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
			    var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
			    var bd_lng = z * Math.cos(theta) + 0.0065;
			    var bd_lat = z * Math.sin(theta) + 0.006;
			    return [bd_lng, bd_lat]
			}
			
			/**
			 * WGS84转GCj02
			 * @param lng
			 * @param lat
			 * @returns {*[]}
			 */
			function wgs84togcj02(lng, lat) {
			    if (out_of_china(lng, lat)) {
			        return [lng, lat]
			    }
			    else {
			        var dlat = transformlat(lng - 105.0, lat - 35.0);
			        var dlng = transformlng(lng - 105.0, lat - 35.0);
			        var radlat = lat / 180.0 * PI;
			        var magic = Math.sin(radlat);
			        magic = 1 - ee * magic * magic;
			        var 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]
			    }
			}
			
			/**
			 * GCJ02 转换为 WGS84
			 * @param lng
			 * @param lat
			 * @returns {*[]}
			 */
			function gcj02towgs84(lng, lat) {
			    if (out_of_china(lng, lat)) {
			        return [lng, lat]
			    }
			    else {
			        var dlat = transformlat(lng - 105.0, lat - 35.0);
			        var dlng = transformlng(lng - 105.0, lat - 35.0);
			        var radlat = lat / 180.0 * PI;
			        var magic = Math.sin(radlat);
			        magic = 1 - ee * magic * magic;
			        var sqrtmagic = Math.sqrt(magic);
			        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
			        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
			        mglat = lat + dlat;
			        mglng = lng + dlng;
			        return [lng * 2 - mglng, lat * 2 - mglat]
			    }
			}
			
			function transformlat(lng, lat) {
			    var 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
			}
			
			function transformlng(lng, lat) {
			    var 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
			}
			
			/**
			 * 判断是否在国内,不在国内则不做偏移
			 * @param lng
			 * @param lat
			 * @returns {boolean}
			 */
			function out_of_china(lng, lat) {
			    return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
			}
			
			
	var zb =  [[117.18081017153435,39.180351072353005],[117.18869831274674,39.172327705878956],[117.19865736657812,39.162310151786585],[117.19867418187522,39.16206056849401],[117.19861652942734,39.161933913947195],[117.19128459695293,39.15406191112996],[117.18436695927721,39.14805767152242],[117.18409784984459,39.14760548894955],[117.18368777833075,39.14736200482281],[117.17579902867737,39.14568051561204],[117.17517219175295,39.145291603176815],[117.17454535482852,39.1447860137979],[117.17409403224167,39.14400817689392],[117.17354912836254,39.142786952618906],[117.17350905478082,39.141150049556416],[117.17354912836254,39.1406216739866],[117.17368305709431,39.13841120991569],[117.17377790333029,39.13808016680974],[117.17456038476769,39.137742991676106],[117.17470265412027,39.13758972971803],[117.17471846182542,39.13651688666937],[117.17471869877892,39.13622798955586],[117.17437302902323,39.12927034970724],[117.17335825329019,39.12055515945039],[117.1734196029791,39.11326106865883],[117.17308446162815,39.100752269326904],[117.1730419555326,39.09499586854889],[117.17611745748218,39.07799577159699],[117.17740039157604,39.072955172895945],[117.17820999825722,39.070710375407],[117.1791583946569,39.0690581588394],[117.1805231602064,39.06781897103181],[117.18235055814654,39.06618464659786],[117.18547441310773,39.06222084933921],[117.18688465058597,39.0597650746997],[117.18764799403016,39.057286351047026],[117.18838820585404,39.05460995027451],[117.18894336472226,39.052436422659895],[117.18909008942717,39.050849520419234],[117.18909008942717,39.048999233208264],[117.18911322104753,39.04718482683441],[117.18914892971304,39.0439627481033],[117.18914892971304,39.03983056922391],[117.18933398266853,39.03785422432469],[117.19016672097013,39.03706367088628],[117.19236308813385,39.03667310300159],[117.19639785932725,39.03594429210341],[117.19971203374354,39.035305706070375],[117.20266653031513,39.034766894616865],[117.2052896766898,39.034282952274765],[117.20811571689006,39.03380400203159],[117.21107021346171,39.03322526615625],[117.21153265640226,39.032985787855836],[117.21150696512876,39.031868211725424],[117.21166111277557,39.02913406704522],[117.21171247447069,39.02725670009224],[117.21168678319526,39.026198908820135],[117.21130141407707,39.0250013901649],[117.21050498456736,39.02412319692388],[117.21009392417375,39.0234645448339],[117.20968286378218,39.02274600828264],[117.20981132015356,39.020550434681695],[117.20997778083591,39.016765575794864],[117.21013623909391,39.013219745822965],[117.21019962239666,39.01065875805031],[117.21023563653279,39.01012983796761],[117.21150330258968,39.008824673767435],[117.21277096864776,39.00759336470858],[117.21391186809927,39.006829942327784],[117.21587675048852,39.00609113863044],[117.21838039095218,39.00532770003693],[117.21940778292611,39.00499564334342],[117.22035853246945,39.00366575664913],[117.2212775903613,39.00154773733959],[117.22257694806956,38.998863179892965],[117.22330585605204,38.99699131810107],[117.22357150165982,38.994245917197304],[117.22357150165982,38.99286656541702],[117.22334966010027,38.991019177169505],[117.2229376686314,38.98860518385564],[117.22235630852248,38.98602480802788],[117.22226123356836,38.98496554228058],[117.22305352485364,38.98370918328854],[117.22416273265497,38.98210790911887],[117.22660298981464,38.98067904928331],[117.2289481720216,38.97942261422247],[117.2312848656959,38.978082757127055],[117.2353097054285,38.97596397212331],[117.23876409543425,38.97404222857523],[117.24033427181308,38.97312217637892]];
            var zhzb =[];
            for(var i = 0; i< zb.length;i++){
            	
             var lng = zb[i][0];
             var lat = zb[i][1];
           //  console.log(lng+"--------------------------"+lat)
             var abn = wgs84togcj02(lng,lat);
            // console.log(abn)
             zhzb.push(abn);
            	
            }
            console.log("高德坐标-gcj02------   "+JSON.stringify(zhzb));
            
            
            
            var baidu =[];
            for(var i = 0; i< zhzb.length;i++){
            	
             var lng = zhzb[i][0];
             var lat = zhzb[i][1];
            // console.log(lng+"--------------------------"+lat)
             var abn = gcj02tobd09(lng,lat);
           //  console.log(abn)
             baidu.push(abn);
            	
            }

             console.log("百度坐标-bd09------   "+JSON.stringify(baidu));
            
		</script>
	</body>
</html>

84坐标直接转换成百度坐标系

javascript 复制代码
            var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
			var PI = 3.1415926535897932384626;
			var a = 6378245.0;
			var ee = 0.00669342162296594323;

			function wgs84tobd(lng, lat) {
			        var dlat = transformlat(lng - 105.0, lat - 35.0);
			        var dlng = transformlng(lng - 105.0, lat - 35.0);
			        var radlat = lat / 180.0 * PI;
			        var magic = Math.sin(radlat);
			        magic = 1 - ee * magic * magic;
			        var 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 lat = lat + dlat;
			        var lng = lng + dlng;
			            var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
					    var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
					    var bd_lng = z * Math.cos(theta) + 0.0065;
					    var bd_lat = z * Math.sin(theta) + 0.006;
					    return [bd_lng, bd_lat]
			}

            function transformlat(lng, lat) {
			    var 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
			}
			
			function transformlng(lng, lat) {
			    var 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
			}

var gg = wgs84tobd(119.86,39.56); //调用转换的方法输入的是84坐标系 返回的是百度坐标系
		console.log(gg);
相关推荐
百万蹄蹄向前冲1 小时前
2024不一样的VUE3期末考查
前端·javascript·程序员
alikami1 小时前
【若依】用 post 请求传 json 格式的数据下载文件
前端·javascript·json
wakangda2 小时前
React Native 集成原生Android功能
javascript·react native·react.js
吃杠碰小鸡2 小时前
lodash常用函数
前端·javascript
emoji1111112 小时前
前端对页面数据进行缓存
开发语言·前端·javascript
一个处女座的程序猿O(∩_∩)O2 小时前
vue3 如何使用 mounted
前端·javascript·vue.js
User_undefined3 小时前
uniapp Native.js原生arr插件服务发送广播到uniapp页面中
android·javascript·uni-app
麦兜*3 小时前
轮播图带详情插件、uniApp插件
前端·javascript·uni-app·vue
陈大爷(有低保)3 小时前
uniapp小案例---趣味打字坤
前端·javascript·vue.js
博客zhu虎康3 小时前
ElementUI 的 form 表单校验
前端·javascript·elementui