convert gps position from double value

*爱你&永不变心* 提交于 2020-01-06 21:14:11

问题


I'm using uniguard UT01 gps car tracker that works with noran protocol the data that gps_tracker send to server has a structure like this: my recieved data is: 52 0 8 0 1 12 0 0 0 0 0 0 1 67 149 240 109 66 194 95 17 66 78 82 48 57 66 49 55 53 55 50 0 0 49 54 45 48 56 45 49 51 32 48 56 58 51 48 58 52 54 0

now i want to get latitude and longitude from this when i convert bytes 15,16,17,18 to longitude, i do this:
i convert 149 240 109 66 to decimal value: 1114501269
then divide it by 30000 : 37150.0423
then divide it again by 60 : 619 and i confused because this doesn't work.
what's the wrong?

expected value must be : 36.3435, 59.4850


回答1:


Try following code

using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] input = {52, 0, 8, 0, 1, 12, 0, 0, 0, 0, 0, 0, 1, 67, 149, 240,
                               109, 66, 194, 95, 17, 66, 78, 82, 48, 57, 66, 49, 55,
                               53, 55, 50, 0, 0, 49, 54, 45, 48, 56, 45, 49, 51, 32, 48, 56, 58, 51, 48, 58, 52, 54, 0
                           };
            GPS gps = new GPS(input);
        }
    }
    public class GPS
    {
        UInt16 packetLen { get; set; }
        UInt16 cmd { get; set; }
        Boolean bEnable { get; set; }
        byte bAlarm { get; set; }
        uint nSpeed { get; set; }
        float nDirection { get; set; }
        float fLongitude { get; set; }
        float fLatitude { get; set; }
        byte[] sUserID { get; set; }
        string userID = "";
        byte[] bDateTime { get; set; }
        string sDateTime = "";
        DateTime dateTime { get; set; }

        public GPS(byte[] bytes)
        {
            packetLen = BitConverter.ToUInt16(bytes, 0);
            cmd = BitConverter.ToUInt16(bytes, 2);
            bEnable = BitConverter.ToBoolean(bytes, 4);
            bAlarm = bytes[5];
            nSpeed = BitConverter.ToUInt32(bytes, 6);
            nDirection = BitConverter.ToSingle(bytes.Skip(10).Take(4).ToArray(),0);
            fLongitude = BitConverter.ToSingle(bytes.Skip(14).Take(4).ToArray(),0);
            fLatitude = BitConverter.ToSingle(bytes.Skip(18).Take(4).ToArray(),0);
            sUserID = bytes.Skip(22).Take(12).ToArray();
            userID = Encoding.ASCII.GetString(sUserID).Replace("\0", "");
            bDateTime = bytes.Skip(34).ToArray();
            sDateTime = Encoding.ASCII.GetString(bDateTime).Replace("\0", "");
            dateTime = DateTime.ParseExact(sDateTime, "yy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
        }

    }


}


来源:https://stackoverflow.com/questions/38931866/convert-gps-position-from-double-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!