这是一篇转载的文章,不过里面的算法我测试了下,挺准的。
一、各个坐标系的概况
众所周知地球是一个不规则椭圆体,GIS中的坐标系定义由基准面和地图投影两组参数确定,而基准面的定义则由特定椭球体及其对应的转换参数确定。 基准面是利用特定椭球体对特定地区地球表面的逼近,因此每个国家或地区均有各自的基准面。基准面是在椭球体基础上建立的,椭球体可以对应多个基准面,而基准面只能对应一个椭球体。意思就是无论是谷歌地图、搜搜地图还是高德地图、百度地图区别只是针对不同的大地地理坐标系标准制作的经纬度,不存在准不准的问题,大家都是准的只是参照物或者说是标准不一样。谷歌地图采用的是WGS84地理坐标系(中国范围除外),谷歌中国地图和搜搜中国地图采用的是GCJ02地理坐标系,百度采用的是BD09坐标系,而设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,为什么不统一用WGS84地理坐标系这就是国家地理测绘总局对于出版地图的要求,出版地图必须符合GCJ02坐标系标准了,也就是国家规定不能直接使用WGS84地理坐标系。所以定位大家感觉不准确很多又叫出版地图为火星地图其实只是坐标系不一样而已。这就是为什么设备采集的经纬度在地图上显示的时候经常有很大的偏差,远远超出民用GPS 10米偏移量的技术规范。
以上参考自: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 反之。
[java] view plaincopy
1. void bd_encrypt(double gg_lat, double gg_lon, double &bd_lat, double &bd_lon)
2.
3. {
4.
5. double x = gg_lon, y = gg_lat;
6.
7. double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
8.
9. double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
10.
11. bd_lon = z * cos(theta) + 0.0065;
12.
13. bd_lat = z * sin(theta) + 0.006;
14.
15. }
16.
17.
18.
19. void bd_decrypt(double bd_lat, double bd_lon, double &gg_lat, double &gg_lon)
20.
21. {
22.
23. double x = bd_lon - 0.0065, y = bd_lat - 0.006;
24.
25. double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
26.
27. double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
28.
29. gg_lon = z * cos(theta);
30.
31. gg_lat = z * sin(theta);
32.
33. }
[java] view plain copy
1. void bd_encrypt(double gg_lat, double gg_lon, double &bd_lat, double &bd_lon)
2.
3. {
4.
5. double x = gg_lon, y = gg_lat;
6.
7. double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
8.
9. double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
10.
11. bd_lon = z * cos(theta) + 0.0065;
12.
13. bd_lat = z * sin(theta) + 0.006;
14.
15. }
16.
17.
18.
19. void bd_decrypt(double bd_lat, double bd_lon, double &gg_lat, double &gg_lon)
20.
21. {
22.
23. double x = bd_lon - 0.0065, y = bd_lat - 0.006;
24.
25. double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
26.
27. double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
28.
29. gg_lon = z * cos(theta);
30.
31. gg_lat = z * sin(theta);
32.
33. }
2.地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法
WGS-84 到 GCJ-02 的转换(即 GPS 加偏)算法
[java] view plaincopy
1. using System;
2.
3. namespace Navi
4. {
5. class EvilTransform
6. {
7. const double pi = 3.14159265358979324;
8.
9. //
10. // Krasovsky 1940
11. //
12. // a = 6378245.0, 1/f = 298.3
13. // b = a * (1 - f)
14. // ee = (a^2 - b^2) / a^2;
15. const double a = 6378245.0;
16. const double ee = 0.00669342162296594323;
17.
18. //
19. // World Geodetic System ==> Mars Geodetic System
20. public static void transform(double wgLat, double wgLon, out double mgLat, out double mgLon)
21. {
22. if (outOfChina(wgLat, wgLon))
23. {
24. mgLat = wgLat;
25. mgLon = wgLon;
26. return;
27. }
28. double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
29. double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
30. double radLat = wgLat / 180.0 * pi;
31. double magic = Math.Sin(radLat);
32. magic = 1 - ee * magic * magic;
33. double sqrtMagic = Math.Sqrt(magic);
34. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
35. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
36. mgLat = wgLat + dLat;
37. mgLon = wgLon + dLon;
38. }
39.
40. static bool outOfChina(double lat, double lon)
41. {
42. if (lon < 72.004 || lon > 137.8347)
43. return true;
44. if (lat < 0.8293 || lat > 55.8271)
45. return true;
46. return false;
47. }
48.
49. static double transformLat(double x, double y)
50. {
51. 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));
52. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
53. ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
54. ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
55. return ret;
56. }
57.
58. static double transformLon(double x, double y)
59. {
60. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
61. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
62. ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
63. ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
64. return ret;
65. }
66. }
67. }
[java] view plain copy
1. using System;
2.
3. namespace Navi
4. {
5. class EvilTransform
6. {
7. const double pi = 3.14159265358979324;
8.
9. //
10. // Krasovsky 1940
11. //
12. // a = 6378245.0, 1/f = 298.3
13. // b = a * (1 - f)
14. // ee = (a^2 - b^2) / a^2;
15. const double a = 6378245.0;
16. const double ee = 0.00669342162296594323;
17.
18. //
19. // World Geodetic System ==> Mars Geodetic System
20. public static void transform(double wgLat, double wgLon, out double mgLat, out double mgLon)
21. {
22. if (outOfChina(wgLat, wgLon))
23. {
24. mgLat = wgLat;
25. mgLon = wgLon;
26. return;
27. }
28. double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
29. double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
30. double radLat = wgLat / 180.0 * pi;
31. double magic = Math.Sin(radLat);
32. magic = 1 - ee * magic * magic;
33. double sqrtMagic = Math.Sqrt(magic);
34. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
35. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
36. mgLat = wgLat + dLat;
37. mgLon = wgLon + dLon;
38. }
39.
40. static bool outOfChina(double lat, double lon)
41. {
42. if (lon < 72.004 || lon > 137.8347)
43. return true;
44. if (lat < 0.8293 || lat > 55.8271)
45. return true;
46. return false;
47. }
48.
49. static double transformLat(double x, double y)
50. {
51. 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));
52. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
53. ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
54. ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
55. return ret;
56. }
57.
58. static double transformLon(double x, double y)
59. {
60. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
61. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
62. ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
63. ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
64. return ret;
65. }
66. }
67. }
以上参考自:http://www.xue5.com/Mobile/iOS/679842.html
3.百度在线转换API
[java] view plaincopy
1. http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=longitude&y=latitude
2. from: 来源坐标系 (0表示原始GPS坐标,2表示Google坐标)
3. to: 转换后的坐标 (4就是百度自己啦,好像这个必须是4才行)
4. x: 精度
5. y: 纬度
[java] view plain copy
1. http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=longitude&y=latitude
2. from: 来源坐标系 (0表示原始GPS坐标,2表示Google坐标)
3. to: 转换后的坐标 (4就是百度自己啦,好像这个必须是4才行)
4. x: 精度
5. y: 纬度
请求之后会返回一串Json
[java] view plaincopy
1. {
2. "error":0,
3. "x":"MTIxLjUwMDIyODIxNDk2",
4. "y":"MzEuMjM1ODUwMjYwMTE3"
5. }
6. error:是结果是否出错标志位,"0"表示OK
7. x: 百度坐标系的精度(Base64加密)
8. y: 百度坐标系的纬度(Base64加密)
[java] view plain copy
1. {
2. "error":0,
3. "x":"MTIxLjUwMDIyODIxNDk2",
4. "y":"MzEuMjM1ODUwMjYwMTE3"
5. }
6. error:是结果是否出错标志位,"0"表示OK
7. x: 百度坐标系的精度(Base64加密)
8. y: 百度坐标系的纬度(Base64加密)
什么情况,经纬度居然还加密?那接下来也只好见招拆招了
[java] view plaincopy
1. import java.io.BufferedReader;
2. import java.io.IOException;
3. import java.io.InputStream;
4. import java.io.InputStreamReader;
5. import java.io.OutputStreamWriter;
6. import java.net.URL;
7. import java.net.URLConnection;
8. import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
9. public class BaiduAPIConverter extends Thread {
10. public static void testPost(String x, String y) throws IOException {
11. try {
12. URL url = new URL("http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x="+ x + "&y=" + y);
13. URLConnection connection = url.openConnection();
14. connection.setDoOutput(true);
15. OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
16. // remember to clean up
17. out.flush();
18. out.close();
19. // 一旦发送成功,用以下方法就可以得到服务器的回应:
20. String sCurrentLine, sTotalString;
21. sCurrentLine = sTotalString = "";
22. InputStream l_urlStream;
23. l_urlStream = connection.getInputStream();
24. BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
25. while ((sCurrentLine = l_reader.readLine()) != null) {
26. if (!sCurrentLine.equals(""))
27. sTotalString += sCurrentLine;
28. }
29. sTotalString = sTotalString.substring(1, sTotalString.length() - 1);
30. String[] results = sTotalString.split("\\,");
31. if (results.length == 3) {
32. if (results[0].split("\\:")[1].equals("0")) {
33. String mapX = results[1].split("\\:")[1];
34. String mapY = results[2].split("\\:")[1];
35. mapX = mapX.substring(1, mapX.length() - 1);
36. mapY = mapY.substring(1, mapY.length() - 1);
37. mapX = new String(Base64.decode(mapX));
38. mapY = new String(Base64.decode(mapY));
39. System.out.println("\t" + mapX + "\t" + mapY);
40. }
41. }
42. sleep(10000);
43. } catch (InterruptedException e) {
44. // TODO Auto-generated catch block
45. e.printStackTrace();
46. }
47. }
48. /**
49. * @param args
50. * @throws IOException
51. */
52. public static void main(String[] args) throws IOException {
53. testPost("120.151379", "30.184678");
54. System.out.println("ok");
55. }
56. }
[java] view plain copy
1. import java.io.BufferedReader;
2. import java.io.IOException;
3. import java.io.InputStream;
4. import java.io.InputStreamReader;
5. import java.io.OutputStreamWriter;
6. import java.net.URL;
7. import java.net.URLConnection;
8. import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
9. public class BaiduAPIConverter extends Thread {
10. public static void testPost(String x, String y) throws IOException {
11. try {
12. URL url = new URL("http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x="+ x + "&y=" + y);
13. URLConnection connection = url.openConnection();
14. connection.setDoOutput(true);
15. OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
16. // remember to clean up
17. out.flush();
18. out.close();
19. // 一旦发送成功,用以下方法就可以得到服务器的回应:
20. String sCurrentLine, sTotalString;
21. sCurrentLine = sTotalString = "";
22. InputStream l_urlStream;
23. l_urlStream = connection.getInputStream();
24. BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
25. while ((sCurrentLine = l_reader.readLine()) != null) {
26. if (!sCurrentLine.equals(""))
27. sTotalString += sCurrentLine;
28. }
29. sTotalString = sTotalString.substring(1, sTotalString.length() - 1);
30. String[] results = sTotalString.split("\\,");
31. if (results.length == 3) {
32. if (results[0].split("\\:")[1].equals("0")) {
33. String mapX = results[1].split("\\:")[1];
34. String mapY = results[2].split("\\:")[1];
35. mapX = mapX.substring(1, mapX.length() - 1);
36. mapY = mapY.substring(1, mapY.length() - 1);
37. mapX = new String(Base64.decode(mapX));
38. mapY = new String(Base64.decode(mapY));
39. System.out.println("\t" + mapX + "\t" + mapY);
40. }
41. }
42. sleep(10000);
43. } catch (InterruptedException e) {
44. // TODO Auto-generated catch block
45. e.printStackTrace();
46. }
47. }
48. /**
49. * @param args
50. * @throws IOException
51. */
52. public static void main(String[] args) throws IOException {
53. testPost("120.151379", "30.184678");
54. System.out.println("ok");
55. }
56. }
到这里也差不多好了,主要的代码都写出来了,其他的您就自己写吧。
以上参考自: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。