座標系簡介
WGS-84:是國際標準,GPS座標(Google Earth使用、或者GPS模塊)
GCJ-02:中國座標偏移標準,Google
Map、高德、騰訊使用 BD-09:百度座標偏移標準,Baidu Map使用
在線gps座標轉百度座標
在百度地圖中,如果直接給出gps座標,將造成偏差,需要將gps座標轉換成百度座標
如在百度地圖中給出gps座標(120.1755556, 35.94)
在線轉換後的百度座標爲
離線gps轉百度地址
參考地址:http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=10923
原文給出的代碼爲java,稍加修改爲C#代碼
測試結果
在給出代碼以前先給出測試結果
與在線轉換結果比較,小數點前4位爲爲精確,在民用中已經足夠
gps座標轉百度座標
相關文件下載:http://download.csdn.net/detail/u014124220/9645257
相關代碼
在線測試代碼
該效果測試html代碼,同時也包含在線座標轉換的方法
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gp2312" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
#l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
#r-result{height:100%;width:20%;float:left;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=9fb983ecd9b505f8fedcc9ab07c65e3e"></script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
<title>GPS轉百度</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
//GPS座標
var xx = 120.1755556;
var yy = 35.94;
var gpsPoint = new BMap.Point(xx,yy);
//地圖初始化
var bm = new BMap.Map("allmap");
bm.centerAndZoom(gpsPoint, 17);
bm.addControl(new BMap.NavigationControl());
//添加谷歌marker和label
var markergps = new BMap.Marker(gpsPoint);
bm.addOverlay(markergps); //添加GPS標註
var labelgps = new BMap.Label("我是GPS標註哦",{offset:new BMap.Size(20,-10)});
markergps.setLabel(labelgps); //添加GPS標註
//座標轉換完之後的回調函數
translateCallback = function (point){
var marker = new BMap.Marker(point);
bm.addOverlay(marker);
var label = new BMap.Label("我是百度標註哦",{offset:new BMap.Size(20,-10)});
marker.setLabel(label); //添加百度label
bm.setCenter(point);
alert("轉化爲百度座標爲:"+point.lng + "," + point.lat);
}
setTimeout(function(){
BMap.Convertor.translate(gpsPoint,0,translateCallback); //真實經緯度轉成百度座標
}, 2000);
</script>
————–離線代碼——————
java和C#代碼一併給出,相關變量如下
pi: 圓周率。 a: 衛星橢球座標投影到平面地圖座標系的投影因子。
ee: 橢球的偏心率。
x_pi: 圓周率轉換量。
transformLat(lat, lon): 轉換方法,比較複雜,不必深究了。輸入:橫縱座標,輸出:轉換後的橫座標。
transformLon(lat, lon): 轉換方法,同樣複雜,自行腦補吧。輸入:橫縱座標,輸出:轉換後的縱座標。
wgs2gcj(lat, lon): WGS座標轉換爲GCJ座標。 gcj2bd(lat, lon): GCJ座標轉換爲百度座標。
離線轉換 C#參考代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mr_newbiebt
{
class gps2bd
{
/*
WGS-84:是國際標準,GPS座標(Google Earth使用、或者GPS模塊)
GCJ-02:中國座標偏移標準,Google Map、高德、騰訊使用
BD-09:百度座標偏移標準,Baidu Map使用
代碼參考:http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=10923
*/
static double pi = 3.14159265358979324;
static double a = 6378245.0;
static double ee = 0.00669342162296594323;
public const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
/// <summary>
/// gps座標轉換成百度座標,小數點前4位爲準確座標
/// </summary>
/// <param name="lat">緯度</param>
/// <param name="lon">經度</param>
/// <returns></returns>
public static double[] wgs2bd(double lat, double lon)
{
double[] dwgs2gcj = wgs2gcj(lat, lon);
double[] dgcj2bd = gcj2bd(dwgs2gcj[0], dwgs2gcj[1]);
return dgcj2bd;
}
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;
}
}
}
離線轉換 java參考代碼
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;
}