Distance calculated by latitude and longitude

Source: Internet
Author: User
Tags cos sin

    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

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.