January 31, 2013 , General ? A total of 2595 words? Small size medium big ? Comments Off
Today in a project need to use the Earth coordinate system to the transformation of the Mars coordinate system, found several methods of implementation, but can not be used in more than iOS6 systems, and finally found a C # conversion algorithm , in contrast to write an iOS version, here to share to you.
The specific meanings of earth coordinates and Mars coordinates can be seen: http://blog.sina.com.cn/s/blog_7581a4c301015230.html
There are several ways to do this online:
1. The earth coordinates are converted to Mars coordinates by _applychinalocationshift in the private class Mklocationmanager on systems prior to iOS4.3. This approach has two problems, one is to call the private API, the other is the system after the iOS5 is not available.
2. Using the Mkmapview in the isshowuserlocation to locate, this method also has two problems, one is to create a mkmapview, the second is unable to achieve background positioning.
3. Using the MAPABC API in the Gpstooffsetbypoint: method for coordinate transformation, I did not try this method, because it is more cumbersome to implement, but also to apply for API Key.
4. Establish a one by one mapping relationship between the Earth coordinate system and the Mars coordinate system, and store the relationship in the database for conversion through the database. The problem with this approach is that the database size is large and does not apply to mobile client programs .
5. Using the online API provided by the High German and Baidu maps, the problem of this method is that it cannot be converted offline and the practicability is not strong.
6. Using existing algorithms to convert the earth coordinate system to the Mars coordinate system
I finally adopted the sixth method, the reference C # algorithm link is as follows: https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936
The resulting OC code is as follows:
const double a = 6378245.0;const double ee = 0.00669342162296594323;+ (cllocation *) Transformtomars: (cllocation *) location { //is outside mainland China if ([[self class] outofchina:location]) { return location; } double dlat = [[self class] transformlatwithx:location.coordinate.longitude - 105.0 y: Location.coordinate.latitude - 35.0]; double dlon = [[self class] transformlonwithx:location.coordinate.longitude - 105.0 y: location.coordinate.latitude - 35.0]; double radlat = Location.coordinate.latitude / 180.0 * m_pi; double magic = sin (Radlat); magic = 1 - ee * magic * magic; double Sqrtmagic = sqrt (Magic); dlat = (dlat * 180.0) / ((a * (1 - ee) / (magic * sqrtmagic) * m_pi); dLon = (dlon * 180.0) / (a / sqrtmagic * cos (radlat) * m_pi); return [[cllocation alloc] Initwithlatitude:location.coordinate.latitude + dlat longitude:location.coordinate.longitude + dlon];} + (BOOL) Outofchina: (cllocation *) location { if ( location.coordinate.longitude < 72.004 | | location.coordinate.longitude > 137.8347) { return YES; } if (location.coordinate.latitude < 0.8293 | | location.coordinate.latitude > 55.8271) { return yes; } return no;} + (Double) Transformlatwithx: (Double) X y: (double) Y { double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y &NBSP;*&NBSP;Y&NBSP;+&NBSP;0.1&NBSP;*&NBSP;X&NBSP;*&NBSP;Y&NBSP;+&NBSP;0.2&NBSP;*&NBSP;SQRT (ABS (x)); ret += (20.0 * sin (6.0&NBSP;*&NBSP;X&NBSP;*&NBSP;M_PI) + 20.0 * sin (2.0&NBSP;*&NBSP;X&NBSP;*&NBSP;M_PI)) * 2.0 / 3.0; ret += (20.0 * sin (Y&NBSP;*&NBSP;M_PI) + 40.0 * sin (y / 3.0 &NBSP;*&NBSP;M_PI) * 2.0 / 3.0; ret += (160.0 *&Nbsp;sin (Y&NBSP;/&NBSP;12.0&NBSP;*&NBSP;M_PI) + 320 * sin (y * M_PI / 30.0)) * 2.0 / 3.0; return ret;} + (Double) Transformlonwithx: (Double) X y: (double) Y { double ret = 300.0 + x + 2.0 * y + 0.1 * x * x &NBSP;+&NBSP;0.1&NBSP;*&NBSP;X&NBSP;*&NBSP;Y&NBSP;+&NBSP;0.1&NBSP;*&NBSP;SQRT (ABS (x)); ret += (20.0 * sin (6.0&NBSP;*&NBSP;X&NBSP;*&NBSP;M_PI) + 20.0 * sin ( 2.0&NBSP;*&NBSP;X&NBSP;*&NBSP;M_PI)) * 2.0 / 3.0; ret += (20.0 * sin (X&NBSP;*&NBSP;M_PI) + 40.0 * sin (X&NBSP;/&NBSP;3.0&NBSP;*&NBSP;M_PI) ) * 2.0 / 3.0; ret += (150.0 * sin (x / &NBSP;12.0&NBSP;*&NBSP;M_PI) &NBSP;+&NBSP;300.0&NBSP;*&NBSp;sin (X&NBSP;/&NBSP;30.0&NBSP;*&NBSP;M_PI)) * 2.0 / 3.0; return ret;}
Put the above code in any class, using the static method Transformtomars: Convert the earth coordinates.
The following is the result of the final conversion:
Mapkit obtained coordinates: 40.006498, 116.328022CLLocationManager coordinates obtained: 40.005196, 116.321890 the coordinates of the algorithm are converted: 40.006500, 116.328023
It can be seen from the above results that the accuracy of the algorithm is high and the error is within 10 meters.
If you feel that you can help, but also hope to help the top, thank you:)
Personal blog: http://blog.csdn.net/zhaoxy2850
This address: http://blog.csdn.net/zhaoxy_thu/article/details/17033347
Reprint please indicate the source, thank you!
IOS7 on earth coordinate system to Mars coordinate system conversion algorithm