关于gps坐标转百度坐标的 js方法以及java方法
2017-09-28 11:01
477 查看
js转换方法
var resultPoint = GpsToBaiduPoint(new BMap.Point(data.lon, data.lat));
var ps = [];
ps.push(new BMap.Point(points[i].lon, points[i].lat));
var resultPoints = GpsToBaiduPoints(ps);
java转换方法
var resultPoint = GpsToBaiduPoint(new BMap.Point(data.lon, data.lat));
var ps = [];
ps.push(new BMap.Point(points[i].lon, points[i].lat));
var resultPoints = GpsToBaiduPoints(ps);
/** * 单个坐标转换 * @param point * @returns {BMap.Point} */ function GpsToBaiduPoint(point){ var _t = wgs2bd(point.lat,point.lng); var _BPoint = new BMap.Point(_t[1], _t[0]); return _BPoint; } /** * 批量坐标转换 * @param points * @returns {Array} */ function GpsToBaiduPoints(points){ var resultPoints = []; $.each(points,function(index,point){ var _t = wgs2bd(point.lat,point.lng); var _BPoint = new BMap.Point(_t[1], _t[0]); resultPoints.push(_BPoint); }); return resultPoints; } ////////////////////////////////////////// //////////////转换核心代码//////////////// ////////////////////////////////////////// var pi = 3.14159265358979324; var a = 6378245.0; var ee = 0.00669342162296594323; var x_pi = 3.14159265358979324*3000.0/180.0; //世界大地坐标转为百度坐标 function wgs2bd(lat,lon) { var wgs2gcjR = wgs2gcj(lat, lon); var gcj2bdR = gcj2bd(wgs2gcjR[0], wgs2gcjR[1]); return gcj2bdR; } function gcj2bd(lat,lon) { var x = lon, y = lat; 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 bd_lon = z * Math.cos(theta) + 0.0065; var bd_lat = z * Math.sin(theta) + 0.006; var result = []; result.push(bd_lat); result.push(bd_lon); return result; } function bd2gcj(lat,lon) { var x = lon - 0.0065, y = 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_lon = z * Math.cos(theta); var gg_lat = z * Math.sin(theta); var result = []; result.push(gg_lat); result.push(gg_lon); return result; } function wgs2gcj(lat,lon) { var dLat = transformLat(lon - 105.0, lat - 35.0); var dLon = transformLon(lon - 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); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi); var mgLat = lat + dLat; var mgLon = lon + dLon; var result = []; result.push(mgLat); result.push(mgLon); return result; } function transformLat(lat,lon) { var ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(Math.abs(lat)); ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0; ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0; ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi / 30.0)) * 2.0 / 3.0; return ret; } function transformLon(lat,lon) { var ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(Math.abs(lat)); ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * 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 += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0; return ret; }
java转换方法
public static void main(String[] args) { // double[] local = GpsToBaiDuXY.wgs2bd(126.50111324097671,45.88024762976107);126.488445 45.871875 double[] local = GpsToBaiDuXY.wgs2bd(45.871875,126.488445); System.out.println(local[0]); System.out.println(local[1]); } static double pi = 3.14159265358979324; static double a = 6378245.0; static double ee = 0.00669342162296594323; public final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0; public static double[] wgs2bd(double lat, double lon) { double[] wgs2gcj = wgs2gcj(lat, lon); double[] gcj2bd = gcj2bd(wgs2gcj[0], wgs2gcj[1]); return gcj2bd; } public static double[] gcj2bd(double lat, double lon) { double x = lon, y = lat; double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); double bd_lon = z * Math.cos(theta) + 0.0065; double bd_lat = z * Math.sin(theta) + 0.006; return new double[] { bd_lat, bd_lon }; } public static double[] bd2gcj(double lat, double lon) { double x = lon - 0.0065, y = lat - 0.006; double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi); double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi); double gg_lon = z * Math.cos(theta); double gg_lat = z * Math.sin(theta); return new double[] { gg_lat, gg_lon }; } public static double[] wgs2gcj(double lat, double lon) { 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; double[] loc = { mgLat, mgLon }; return loc; } private static double transformLat(double lat, double lon) { double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(Math.abs(lat)); ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0; ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0; ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi / 30.0)) * 2.0 / 3.0; return ret; } private static double transformLon(double lat, double lon) { double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(Math.abs(lat)); ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * 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 += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0; return ret; }
相关文章推荐
- 关于GPS坐标转百度坐标与goolg坐标转百度坐标java代码实现方法
- 把Gps坐标转成百度坐标的java方法
- 真实经纬度(gps)转成百度坐标的js方法
- 关于GPS定位经纬度到谷歌(google)坐标以及百度(百度)坐标的转换c#源码
- 真实经纬度(gps)转成百度坐标的js方法
- android学习笔记---53_采用网页设计软件界面,以及使用android系统内置的浏览器,利用js调用java方法
- 解析关于java,php以及html的所有文件编码与乱码的处理方法汇总
- 解析关于java,php以及html的所有文件编码与乱码的处理方法汇总
- jsp引用css和js文件方法 以及 java web应用程序文件结构
- android中webview携带cookie以及webview所加载网页中js调用java方法问题
- 关于java基础--基本数据类型封装以及String类方法讲解
- GPS坐标转换为百度坐标的方法
- 关于UIView的坐标转换以及好用方法
- 关于java中main()方法以及命令行参数的若干注意
- JAVA 关于rotate()以后坐标以及显示的问题
- 关于在jsp中java代码和js以及html代码的执行顺序
- java调用js脚本,以及js脚本调用java方法
- 关于java方法的重载(Overloading),覆写(Override)以及final 方法的几点说明
- 百度 谷歌 GPS 坐标互转 java版