<span id="Label3"></p><p><p>Given the latitude and longitude of two points, calculate the distance between two points, here to pay attention to latitude and longitude must be filled in order</p></p><p><p>1. Use the tools in Android to get the units in meters</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">float[] results=new float[1]; Location.distancebetween (latitude,longitude,double.parsedouble (lat), double.parsedouble (lng), results); LOG.I ("distance", results[0]+ "");</pre></pre><p><p></p></p><p><p>2. Obtained by means of calculation, the unit is kilometer</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Private static final Double Earth_radius = 6378.137; private static double rad (double d) { return d * math.pi/180.0; } /** * Calculates the distance between two points according to the latitude and longitude coordinates (double Value) between two points, * * @param lat1 * @param lng1 * @param lat2 * @param lng2< c10/>* @return distance: kilometers per km /public static double distanceoftwopoints (double lat1,double lng1, Double Lat2,double lng2) { Double radLat1 = rad (lat1); Double radLat2 = rad (lat2); Double A = radlat1-radlat2; Double b = rad (lng1)-rad (lng2); Double s = 2 * math.asin (math.sqrt (math.pow (math.sin (a/2), 2) + math.cos (radLat1) * Math.Cos (radLat2) * MATH.P ow (math.sin (b/2), 2))); s = S * earth_radius; s = Math.Round (s * 10000)/10000; LOG.I ("distance", s+ ""); return s; }</pre></pre><br><br><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Calculate distance for Android given coordinates</p></p></span>
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