Wednesday, March 12, 2014

[C#] NMEA GPS format comvert to Google Maps format (WGS84)


Here is a simple example to show how to convert GPS coordinate from GPS receiver(NMEA format) to Google Maps(WGS84 format).

這是一個簡單的範例,秀出如何將GPS座標從GPS接收器(NMEA格式)轉成Google地圖可用的(WGS84格式)

    public class Coordinate
    {
        public decimal Latitude { get; set; }
        public decimal Longitude { get; set; }
    }

    public class Tools
    {
        /// 
        /// Convert NMEA format to Google map format
        /// 
        /// Latitude
        /// Longitude
        /// Example : 2503.5612N, 12136.6334E
        /// Output : {"Latitude":25.059353,"Longitude":121.610557}
        public static Coordinate NmeaToGoogle(string lat, string lon)
        {
            decimal latitude = decimal.Parse(lat.Substring(0, 2))
                              + decimal.Parse(lat.Substring(2, 7)) / 60;
            if (lat.Substring(9, 1).ToLower() == "s") latitude = latitude*-1;

            decimal longitude = decimal.Parse(lon.Substring(0, 3))
                               + decimal.Parse(lon.Substring(3, 7)) / 60;
            if (lon.Substring(10, 1).ToLower() == "w") longitude = longitude * -1;

            return new Coordinate { Latitude = Decimal.Round(latitude, 6), 
                                    Longitude = Decimal.Round(longitude,6) };
        }
    }


ref:
National Marine Electronics Association
World Geodetic System

3 comments:

  1. Much obliged to you for some other instructive site. The spot else might just I get that sort of data written in such a flawless strategy? I have an endeavor that I am basically now running on, and I've been at the look out for such information. maps126

    ReplyDelete