计算可以计算出结果,但是根据http://www.gpsspg.com/maps.htm进行测试,在84和火星坐标系进行转换时候,精度相差了好几百米。
1 /** 2 * Created by hasee on 2017/7/31. 3 */ 4 public class Transform { 5 double x_PI = 3.14159265358979324 * 3000.0 / 180.0; 6 double PI = 3.1415926535897932384626; 7 double a = 6378245.0; 8 double ee = 0.00669342162296594323; 9 10 /** 11 * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换 12 * 即 百度 转 谷歌、高德 13 * @param bd_lon 14 * @param bd_lat 15 * @returns {*[]} 16 */ 17 public Point bd09togcj02(double bd_lon, double bd_lat){ 18 double x = bd_lon - 0.0065; 19 double y = bd_lat - 0.006; 20 double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_PI); 21 double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_PI); 22 double gg_lng = z * Math.cos(theta); 23 double gg_lat = z * Math.sin(theta); 24 Point point=new Point(gg_lng, gg_lat); 25 return point; 26 } 27 28 /** 29 * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换 30 * 即谷歌、高德 转 百度 31 * @param lng 32 * @param lat 33 * @returns {*[]} 34 */ 35 public Point gcj02tobd09(double lng, double lat){ 36 double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI); 37 double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI); 38 double bd_lng = z * Math.cos(theta) + 0.0065; 39 double bd_lat = z * Math.sin(theta) + 0.006; 40 Point point=new Point(bd_lng, bd_lat); 41 return point; 42 }; 43 44 /** 45 * WGS84转GCj02 46 * @param lng 47 * @param lat 48 * @returns {*[]} 49 */ 50 public Point wgs84togcj02(double lng, double lat){ 51 double dlat = transformlat(lng - 105.0, lat - 35.0); 52 double dlng = transformlng(lng - 105.0, lat - 35.0); 53 double radlat = lat / 180.0 * PI; 54 double magic = Math.sin(radlat); 55 magic = 1 - ee * magic * magic; 56 double sqrtmagic = Math.sqrt(magic); 57 dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI); 58 dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI); 59 double mglat = lat + dlat; 60 double mglng = lng + dlng; 61 Point point=new Point(mglng, mglat); 62 return point; 63 }; 64 65 /** 66 * GCJ02 转换为 WGS84 67 * @param lng 68 * @param lat 69 * @returns {*[]} 70 */ 71 public Point gcj02towgs84(double lng, double lat){ 72 double dlat = transformlat(lng - 105.0, lat - 35.0); 73 double dlng = transformlng(lng - 105.0, lat - 35.0); 74 double radlat = lat / 180.0 * PI; 75 double magic = Math.sin(radlat); 76 magic = 1 - ee * magic * magic; 77 double sqrtmagic = Math.sqrt(magic); 78 dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI); 79 dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI); 80 double mglat = lat + dlat; 81 double mglng = lng + dlng; 82 Point point=new Point(mglng, mglat); 83 return point; 84 }; 85 86 87 private double transformlat(double lng,double lat){ 88 double ret= -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng)); 89 ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0; 90 ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0; 91 ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0; 92 return ret; 93 } 94 95 private double transformlng(double lng,double lat){ 96 double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng)); 97 ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0; 98 ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0; 99 ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0; 100 return ret; 101 } 102 103 }
在前端js使用举例***********************************************************************
<div> <% /** 取出在数据库中存的百度坐标 */ String lat=new String( request.getParameter("lat").getBytes("ISO8859-1"),"UTF-8"); String lng=new String( request.getParameter("lng").getBytes("ISO8859-1"),"UTF-8"); /** 百度坐标转换为火星坐标 */ double x_PI = 3.14159265358979324 * 3000.0 / 180.0; double x =Double.parseDouble(lng) - 0.0065; double y = Double.parseDouble(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_lng = z * Math.cos(theta); double gg_lat = z * Math.sin(theta); // out.print("gg_lat"+gg_lat); // out.print("gg_lng"+gg_lng); %> </div>
好人做到底,下面是直接调用高德地图导航的代码,(就是从当前位置导航数据库中查出来的目标位置)*****************************************
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
<title>浏览器定位</title>
<style type="text/css">
#panel {
position: absolute;
background-color: white;
max-height: 80%;
overflow-y: auto;
top: 10px;
right: 10px;
250px;
border: solid 1px silver;
}
</style>
</head>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.3&key=你申请的KEY&plugin=AMap.Driving&plugin=AMap.ToolBar&plugin=AMap.Walking&plugin=AMap.Transfer"></script>
<script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
<body>
<div id='container2'></div>
<div id="container"></div>
<div id="panel">
<%
String lat=new String( request.getParameter("lat").getBytes("ISO8859-1"),"UTF-8");
String lng=new String( request.getParameter("lng").getBytes("ISO8859-1"),"UTF-8");
/**
百度坐标转换为火星坐标
*/
double x_PI = 3.14159265358979324 * 3000.0 / 180.0;
double x =Double.parseDouble(lng) - 0.0065;
double y = Double.parseDouble(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_lng = z * Math.cos(theta);
double gg_lat = z * Math.sin(theta);
// out.print("gg_lat"+gg_lat);
// out.print("gg_lng"+gg_lng);
%>
</div>
<script type="text/javascript">
/***************************************
由于Chrome、IOS10等已不再支持非安全域的浏览器定位请求,为保证定位成功率和精度,请尽快升级您的站点到HTTPS。
***************************************/
var map, geolocation;
//加载地图,调用浏览器定位服务
map = new AMap.Map('container', {
resizeEnable: true,
center: [116.397428, 39.90923],
zoom: 13 //地图显示的缩放级别
});
var transOptions = {
map: map,
city: '北京市',
panel: 'panel',
//cityd:'乌鲁木齐',
policy: AMap.TransferPolicy.LEAST_TIME
};
map.plugin('AMap.Geolocation', function() {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:无穷大
buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
zoomToAccuracy: false, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
buttonPosition:'RB'
});
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
});
//构造公交换乘类
var transfer = new AMap.Transfer(transOptions);
//构造路线导航类
// var driving = new AMap.Walking({
// map: map
// panel: "panel"
// });
//解析定位结果
function onComplete(data) {
// 根据起终点经纬度规划驾车导航路线
transfer.search(new AMap.LngLat(data.position.getLng(), data.position.getLat()), new AMap.LngLat(<%=gg_lng%>,<%=gg_lat%>));
}
</script>
</body>
</html>
百度导航调用也直接上代码*********************************
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page import="java.net.URLEncoder" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0"> <title>调用百度地图,获取当前位置,导航目的地</title> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你申请的Key"></script> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script><!--调用jQuery--> <style type="text/css"> body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";} </style> </head> <body> <div id="allmap"> <%
/** 取出在数据库中存的百度坐标 */ String lat=new String( request.getParameter("lat").getBytes("ISO8859-1"),"UTF-8"); String lng=new String( request.getParameter("lng").getBytes("ISO8859-1"),"UTF-8");%> </div> </body> <script type="text/javascript"> var map = new BMap.Map("allmap"); var point = new BMap.Point(116.709684,39.89778); map.centerAndZoom(point, 16); map.enableScrollWheelZoom(); var myIcon = new BMap.Icon("myicon.png",new BMap.Size(30,30),{ anchor: new BMap.Size(10,10) }); var marker=new BMap.Marker(point,{icon: myIcon}); map.addOverlay(marker); var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point); map.addOverlay(mk); //map.panTo(r.point);//地图中心点移到当前位置 var latCurrent = r.point.lat; var lngCurrent = r.point.lng; //alert('我的位置:'+ latCurrent + ',' + lngCurrent); location.href="http://api.map.baidu.com/direction?origin="+latCurrent+","+lngCurrent+"&destination=<%=lat%>,<%=lng%>&mode=driving®ion=北京&output=html"; } else { alert('failed'+this.getStatus()); } },{enableHighAccuracy: true}) map.addOverlay(marker); var licontent="<b>"+<%=dname%>+"</b><br>"; licontent+="<span><strong>"+<%=addr%>+"</span><br>"; licontent+="<span><strong>电话:</strong>(010)81556565 / 6969</span><br>"; var opts = { width : 200, height: 80, }; var infoWindow = new BMap.InfoWindow(licontent, opts); marker.openInfoWindow(infoWindow); marker.addEventListener('click',function(){ marker.openInfoWindow(infoWindow); }); </script> </html>