源地址:http://blog.csdn.net/yorling/article/details/9175913
一、各個座標系的概況
以上參考自:haotsp.com
總結:
WGS84座標系:即地球座標系,國際上通用的座標系。
GCJ02座標系:即火星座標系,WGS84座標系經加密後的座標系。
BD09座標系:即百度座標系,GCJ02座標系經加密後的座標系。
搜狗座標系、圖吧座標系等,估計也是在GCJ02基礎上加密而成的。
二、各個地圖API採用的座標系
API | 座標系 |
百度地圖API | 百度座標 |
騰訊搜搜地圖API | 火星座標 |
搜狐搜狗地圖API | 搜狗座標* |
阿里雲地圖API | 火星座標 |
圖吧MapBar地圖API | 圖吧座標 |
高德MapABC地圖API | 火星座標 |
靈圖51ditu地圖API | 火星座標 |
注1:百度地圖使用百度座標,支持從地球座標和火星座標導入成百度座標,但無法導出。並且批量座標轉換一次只能轉換20個(待驗證)。
注2:搜狗地圖支持直接顯示地球座標,支持地球座標、火星座標、百度座標導入成搜狗座標,同樣,搜狗座標也無法導出。
個人認爲:採用自家座標體系,而不採用國內通用的火星座標體系,實在是自尋短處。當然,百度是因爲做的足夠大、足夠好,所以很霸道,也爲以後一統天下而不讓別人瓜分之而做準備吧。搜狗雖然用自家座標體系,但能將地球座標直接導入,此舉也屬唯一。而圖吧地圖不知道學什麼加密方式,以前用地球座標用的好好的,現在用圖吧自己的座標,難道是因爲給百度做過所以也來了這麼一招?或者沿用百度?不得而知。
本文的目的在於:做地圖開發的時候,不希望被一家地圖API遷就,所以採用火星座標是正確的選擇,希望本文能夠對選擇使用誰家API的開發者提供一點幫助吧。就我個人而言,我絕不會使用非火星座標系統的地圖API,雖然百度地圖API很好很強大確實很吸引我。
以上參考自:http://rovertang.com/labs/map-compare/
三、各個座標系的相互轉換
1.火星座標系 (GCJ-02) 與百度座標系 (BD-09) 的轉換算法,其中 bd_encrypt
將 GCJ-02 座標轉換成 BD-09 座標, bd_decrypt
反之。
- void bd_encrypt(double gg_lat, double gg_lon, double &bd_lat, double &bd_lon)
- {
- double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
- double x = gg_lon, y = gg_lat;
- double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
- double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
- bd_lon = z * cos(theta) + 0.0065;
- bd_lat = z * sin(theta) + 0.006;
- }
- void bd_decrypt(double bd_lat, double bd_lon, double &gg_lat, double &gg_lon)
- {
- double x = bd_lon - 0.0065, y = bd_lat - 0.006;
- double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
- double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
- gg_lon = z * cos(theta);
- gg_lat = z * sin(theta);
- }
2.地球座標系 (WGS-84) 到火星座標系 (GCJ-02) 的轉換算法
WGS-84 到 GCJ-02 的轉換(即 GPS 加偏)算法
- using System;
- namespace Navi
- {
- class EvilTransform
- {
- const double pi = 3.14159265358979324;
- //
- // Krasovsky 1940
- //
- // a = 6378245.0, 1/f = 298.3
- // b = a * (1 - f)
- // ee = (a^2 - b^2) / a^2;
- const double a = 6378245.0;
- const double ee = 0.00669342162296594323;
- //
- // World Geodetic System ==> Mars Geodetic System
- public static void transform(double wgLat, double wgLon, out double mgLat, out double mgLon)
- {
- if (outOfChina(wgLat, wgLon))
- {
- mgLat = wgLat;
- mgLon = wgLon;
- return;
- }
- double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
- double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
- double radLat = wgLat / 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);
- mgLat = wgLat + dLat;
- mgLon = wgLon + dLon;
- }
- static bool outOfChina(double lat, double lon)
- {
- if (lon < 72.004 || lon > 137.8347)
- return true;
- if (lat < 0.8293 || lat > 55.8271)
- return true;
- return false;
- }
- static double transformLat(double x, double y)
- {
- double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));
- ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
- ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
- ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
- return ret;
- }
- static double transformLon(double x, double y)
- {
- double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
- ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
- ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
- ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
- return ret;
- }
- }
- }
3.百度在線轉換API
- http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=longitude&y=latitude
- from: 來源座標系 (0表示原始GPS座標,2表示Google座標)
- to: 轉換後的座標 (4就是百度自己啦,好像這個必須是4才行)
- x: 精度
- y: 緯度
請求之後會返回一串Json
- {
- "error":0,
- "x":"MTIxLjUwMDIyODIxNDk2",
- "y":"MzEuMjM1ODUwMjYwMTE3"
- }
- error:是結果是否出錯標誌位,"0"表示OK
- x: 百度座標系的精度(Base64加密)
- y: 百度座標系的緯度(Base64加密)
什麼情況,經緯度居然還加密?那接下來也只好見招拆招了
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.URL;
- import java.net.URLConnection;
- import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
- public class BaiduAPIConverter extends Thread {
- public static void testPost(String x, String y) throws IOException {
- try {
- URL url = new URL("http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x="+ x + "&y=" + y);
- URLConnection connection = url.openConnection();
- connection.setDoOutput(true);
- OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
- // remember to clean up
- out.flush();
- out.close();
- // 一旦發送成功,用以下方法就可以得到服務器的迴應:
- String sCurrentLine, sTotalString;
- sCurrentLine = sTotalString = "";
- InputStream l_urlStream;
- l_urlStream = connection.getInputStream();
- BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
- while ((sCurrentLine = l_reader.readLine()) != null) {
- if (!sCurrentLine.equals(""))
- sTotalString += sCurrentLine;
- }
- sTotalString = sTotalString.substring(1, sTotalString.length() - 1);
- String[] results = sTotalString.split("\\,");
- if (results.length == 3) {
- if (results[0].split("\\:")[1].equals("0")) {
- String mapX = results[1].split("\\:")[1];
- String mapY = results[2].split("\\:")[1];
- mapX = mapX.substring(1, mapX.length() - 1);
- mapY = mapY.substring(1, mapY.length() - 1);
- mapX = new String(Base64.decode(mapX));
- mapY = new String(Base64.decode(mapY));
- System.out.println("\t" + mapX + "\t" + mapY);
- }
- }
- sleep(10000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- testPost("120.151379", "30.184678");
- System.out.println("ok");
- }
- }
到這裏也差不多好了,主要的代碼都寫出來了,其他的您就自己寫吧。
以上參考自:http://scalpel.me/archives/136/
四、重點啊,原來百度有內置轉換方法,這下可以不侷限於百度定位SDK了
在百度地圖中取得WGS-84座標,調用如下方法:
BMapManager.getLocationManager().setLocationCoordinateType(MKLocationManager.MK_COORDINATE_WGS84);
這樣從百度api中取得的座標就是WGS-84了,可是這種座標如果顯示到百度地圖上就會偏移,也就是說取出一個座標,原封不動的顯示上去就偏移了,所以爲了顯示也是正常就需要在繪製到百度地圖上之前轉換成BD-09。
轉換成BD-09,調用方法:
GeoPoint wgs84;
GeoPoint bd09 = CoordinateConvert.bundleDecode(CoordinateConvert.fromWgs84ToBaidu(wgs84));
這裏實在不明白爲何要設計成CoordinateConvert.fromWgs84ToBaidu(wgs84)返回了一個Bundle,所以還需要CoordinateConvert.bundleDecode()再轉成GeoPoint。