[C #] the use of correction data to deal with the Earth coordinate (WGS-84) and Mars coordinate (GCJ-02) conversion,

Source: Internet
Author: User

[C #] the use of correction data to deal with the Earth coordinate (WGS-84) and Mars coordinate (GCJ-02) conversion,

Key code:

Using System; using System. collections; using System. collections. generic; using System. IO; using YanZhiwei. dotNet2.Utilities. models; namespace YanZhiwei. dotNet2.Utilities. common {/// <summary> // map correction data help class // </summary> public class MapOffsetDataHelper {# region constructor and variable private string offsetFullPath = string. empty; /// <summary> /// constructor /// </summary> /// <param name = "path"> correction data file path </param> public MapOffsetDataHelper (string path) {offsetFullPath = path ;}# endregion # region private method private void GetOffsetData (Action <MapCoord> mapCoordHanlder) {using (FileStream stream = new FileStream (offsetFullPath, FileMode. openOrCreate, FileAccess. read) {using (BinaryReader reader = new BinaryReader (stream) {int _ size = (int) stream. length/8; for (int I = 0; I <_ size; I ++) {byte [] _ source = reader. readBytes (8); MapCoord _ coord = ToCoord (_ source); mapCoordHanlder (_ coord );}}}} /// <summary> /// convert bytes to a specific data object // </summary> /// <param name = "bytes"> bytes </param> /// <returns> MapCoord </returns> private MapCoord ToCoord (byte [] bytes) {// longitude, latitude, x offset, y offset [Both bytes] MapCoord _ coord = new MapCoord (); byte [] _ b1 = new byte [2], _ b2 = new byte [2], _ b3 = new byte [2], _ b4 = new byte [2]; Array. copy (bytes, 0, _ b1, 0, 2); Array. copy (bytes, 2, _ b2, 0, 2); Array. copy (bytes, 4, _ b3, 0, 2); Array. copy (bytes, 6, _ b4, 0, 2); _ coord. lon = BitConverter. toInt16 (_ b1, 0); _ coord. lat = BitConverter. toInt16 (_ b2, 0); _ coord. x_off = BitConverter. toInt16 (_ b3, 0); _ coord. y_off = BitConverter. toInt16 (_ b4, 0); return _ coord ;} # endregion # region obtains the rectification Data Set /// <summary> // obtains the rectification Data Set /// </summary> /// <returns> correction data set </returns> public List <MapCoord> GetMapCoordList () {List <MapCoord> _ mapCoordList = new List <MapCoord> (); GetOffsetData (c => _ mapCoordList. add (c); return _ mapCoordList ;} /// <summary> /// get the rectification Data Set /// </summary> /// <returns> correction data set </returns> public ArrayList GetMapCoordArrayList () {ArrayList _ mapCoordArrayList = new ArrayList (); GetOffsetData (c => _ mapCoordArrayList. add (c); return _ mapCoordArrayList; }# endregion }}

Bytes -----------------------------------------------------------------------------------------------

Using System. collections; using YanZhiwei. dotNet2.Utilities. models; using YanZhiwei. dotNet2.Utilities. tool; namespace YanZhiwei. dotNet2.Utilities. common {/// <summary> /// map correction help class /// </summary> public class MapOffsetHelper {/** reference: * 1. http://www.apkbus.com/forum.php?mod=viewthread&tid=137621&extra=page%3D1&page=1 * 2. http://yanue.net/post-122.html * 3. http://go2log.com/2011/08/30/%E4%B8%AD%E5%9B%BD%E5%9C%B0%E5%9B%BE%E5%81%8F%E7%A7%BB%E6%A0%A1%E6%AD%A3php%E7%AE%97%E6%B3%95/ * 4. http://www.devdiv.com/ios_gps_google_-blog-60266-10835.html */# Region constructor and variable private ArrayList mapCoordArrayList; /// <summary> /// constructor /// </summary> /// <param name = "offsetData"> correction data </param> public MapOffsetHelper (ArrayList offsetData) {mapCoordArrayList = offsetData;} # endregion # region private method private MapCoord QueryOffSetData (LatLngPoint point) {MapCoord _ search = new MapCoord (); _ search. lat = (int) (point. latY * 100); _ search. lon = (int) (point. lonX * 100); MapOffsetComparer rc = new MapOffsetComparer (); int _ findedIndex = mapCoordArrayList. binarySearch (0, mapCoordArrayList. count, _ search, rc); MapCoord _ findedCoord = (MapCoord) mapCoordArrayList [_ findedIndex]; return _ findedCoord;} # endregion # region Earth coordinate (WGS-84) to the Mars coordinate (GCJ-02) // <summary> // Earth coordinate (WGS-84) to the Mars coordinate (GCJ-02) /// </summary> /// <param name = "wgsPoint"> Earth coordinate (WGS-84) </param> // <returns> Mars coordinate (GCJ-02) </returns> public LatLngPoint WGS84ToGCJ02 (LatLngPoint wgsPoint) {MapCoord _ findedCoord = QueryOffSetData (wgsPoint); double _ pixY = MapHelper. latToPixel (wgsPoint. latY, 18); double _ pixX = MapHelper. lonToPixel (wgsPoint. lonX, 18); _ pixY + = _ findedCoord. y_off; _ pixX + = _ findedCoord. x_off; double _ lat = MapHelper. pixelToLat (_ pixY, 18); double _ lng = MapHelper. pixelToLon (_ pixX, 18); return new LatLngPoint (_ lat, _ lng);} # endregion # region Mars Coordinate Transfer (GCJ-02) Earth coordinate (WGS-84) /// <summary> // Mars Coordinate Transfer (GCJ-02) Earth coordinate (WGS-84) /// </summary> /// <param name = "gcjPoint"> (GCJ-02) </param> // <returns> (WGS-84) </returns> public LatLngPoint GCJ02ToWGS84 (LatLngPoint gcjPoint) {MapCoord _ findedCoord = QueryOffSetData (gcjPoint); double _ pixY = MapHelper. latToPixel (gcjPoint. latY, 18); double _ pixX = MapHelper. lonToPixel (gcjPoint. lonX, 18); _ pixY-= _ findedCoord. y_off; _ pixX-= _ findedCoord. x_off; double _ lat = MapHelper. pixelToLat (_ pixY, 18); double _ lng = MapHelper. pixelToLon (_ pixX, 18); return new LatLngPoint (_ lat, _ lng) ;}# endregion }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.