The first few days of contact with a longitude and latitude computing distance algorithm, very concise Oh.
When the Zennai is in operation, it reports the result of the wrong type. Later, after analysis, the problem was found in 13 lines in the code below.
/ * Calculation of two GPS points straight distance result unit: M
3 */4 Public Static DoubleDistanceDoubleJINGDU1,DoubleWEIDU1,5 DoubleJINGDU2,Doubleweidu2) {6 Doubletheta = jingdu1-Jingdu2;7 DoubleDist = Math.sin (Deg2rad (WEIDU1)) *Math.sin (Deg2rad (WEIDU2))8+ Math.Cos (Deg2rad (WEIDU1)) *Math.Cos (Deg2rad (WEIDU2))9*Math.Cos (Deg2rad (theta));
10
11
A -Dist =Math.acos (dist); -Dist =rad2deg (dist); the returnDIST * 60 * 1.1515 * 1.609344 * 1000; - } - //convert an angle to radians - Static DoubleDeg2rad (Doubledegree) { + returndegree/180 *Math.PI; - } + A //convert radians to angles at Static DoubleRad2deg (Doubleradian) { - returnRadian * 180/Math.PI; -}
The 7th line gets the dist, and the inverse cosine is calculated on line 13th. But in a specific latitude and longitude (the probability is particularly small, unfortunately, not written down ), you will get a value of 1.00000000000000000001. we know that the cosine value can only be 1, so the inverse cosine will be treated as a mistake . So in 10, 11, 12 line with the following code is OK!
dist = 1.0; }
Distance calculated by latitude and longitude