Convert decimal to latitude and longitude
algorithm, delphi, math
Solution
`numar` is specified in 1/500th of a second. So, the following equations hold:
num/500 = seconds
num/500/60 = minutes
num/500/60/60 = degrees
I would calculate it all like this:
var
degrees, minutes, seconds: Integer;
....
degrees := num div (500*60*60);
minutes := num div (500*60) - degrees*60;
seconds := num div 500 - minutes*60 - degrees*60*60;
If you need to calculate the fractional part of seconds then do it like this. Note that there's simply no need for `Extended` here.
var
degrees, minutes: Integer;
seconds: Double;
....
degrees := num div (500*60*60);
minutes := num div (500*60) - degrees*60;
seconds := num/500 - minutes*60 - degrees*60*60;
Plug your value of 40582974 into these formula and the results are:
degrees: 22
minutes: 32
seconds: 45
Judging from comments what you actually want is degrees as an integer and minutes as a floating point. That you can do like this:
var
degrees: Integer;
minutes: Double;
....
degrees := num div (500*60*60);
minutes := num/(500*60) - degrees*60;
Plug your value of 40582974 into these formula and the results are:
degrees: 22
minutes: 32.7658
Problem
I have a GPS device which sends data to my server, I need to convert the decimal values that the device sends into latitude and longitude. I am bad at math so all my attempts failed, here are the specs: Latitude Occupy 4 bytes, representing the latitude value. Number range is from 0 to 162000000, which represents the range form 0°to 90°. Unit: 1/500 second Conversion method: A) Convert the latitude (degrees, minutes) data from GPS module into a new form which represents the value only in minutes; B Multiply the converted value by 30000, and then transform the result to hexadecimal number For example22°32.7658′,(22×60+32.7658)×30000=40582974, then convert it to hexadecimal number 0x02 0x6B 0x3F 0x3E Longitude Occupy 4 bytes, representing the longitude value of location data. Number ranges from 0 to 324000000, representing the range form 0°to 180°.Unit: 1/500 seconds, Conversion method is the same as latitude’s. I came up with this function but it doesn't seem to work: ``` procedure GetDegree(const numar : DWORD; out min,sec : Extended); var eu : Extended; begin eu := numar / 30000; min := Trunc(eu / 60); sec := eu - min * 60; end; ```