The most recent project needs to calculate the distance between the user and the merchant, and requires a specific distance to be displayed in the ListView. Think about the general idea, the first is to get the current user's latitude and longitude, in a specific business to get the latitude and longitude (service-side merchants latitude and longitude has been provided), according to the latitude and longitude of users and businesses to calculate the actual distance between the current user and business. This paper studies the concrete realization method, calculates two points distance generally has two kinds of realization way:
One is calculated using the Pythagorean theorem;
One is to calculate the spherical distance between two points;
Two methods of calculation I made a comparison, when the distance of two points is nearly the same as the results of the two methods, but when the distance between two points is far away, the results of the calculation will be very far apart, it is found that the method of calculating the spherical distance between two points is more accurate and closer to the actual distance. This paper focuses on the calculation method of the spherical distance between two points.
First, let's look at the comparison results:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/74/45/wKioL1YX0ALSHO7DAAHHQdik6QA250.jpg "title=" Baidu test. png "alt=" wkiol1yx0alsho7daahhqdik6qa250.jpg "/>
Test results:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/74/45/wKioL1YX0JCgsMbSAADOs-vV-Xg166.jpg "title=" Ceshi.png "alt=" Wkiol1yx0jcgsmbsaados-vv-xg166.jpg "/>
The comparison results showed that the distance between the spherical distance and two points was closer to the actual distance.
principle of calculation of spherical distance between one and two points
The method is as follows:
As shown on the right, if angle AOB (globe angle) is θ, the radius of the ball is R,a point dimension, longitude is 650) this.width=650; "title=" "height=" "width=" "src="/http d.hiphotos.baidu.com/baike/s%3d47/sign=54fca584a8345982c18ae4950df4dc95/ C9fcc3cec3fdfc035164d248d13f8794a4c22625.jpg "style=" border:0px; "/>, B-point dimension, longitude
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/74/45/wKioL1YXzvXTs6-vAAA6yNtMUj8129.jpg "title=" Qiu.png "style=" float:right; "alt=" wkiol1yxzvxts6-vaaa6yntmuj8129.jpg "/>
650) this.width=650, "title=", "height=", width= "src=" http://h.hiphotos.baidu.com/baike/s%3D47/sign= 70f06499202dd42a5b0900ac013bf8cc/bd3eb13533fa828b01c4ac13f81f4134960a5a5e.jpg "style=" border:0px; "/>, The spherical distance is rθ
Spherical distance Calculation formula: D (x1,y1,x2,y2) =r*arccos (sin (x1) *sin (x2) +cos (x1) *cos (x2) *cos (y1-y2))
X1,y1 is the radian unit of latitude and longitude, and R is the radius of the Earth.
When Y1=y2, the formula becomes:
d=r*|x1-x2|
Second, the specific implementation of the Code
package com.sunny.test;public class testdistance {static double def_pi = 3.14159265359; // PI3.141592653589793static double DEF_2PI= 6.28318530712; // 2*PIstatic double DEF_PI180= 0.01745329252; // PI/180.0static double def_r =6370693.5; // Earth radius public static void main (String[] args) {//Urumqi 87.49033,43.962242 Beijing 116.337291,40.01116double distance1 = getlongdistance (87.49033,43.962242, 116.337291,40.01116)/1000;double distance2 = Getshortdistance (87.49033,43.962242, 116.337291,40.01116)/1000 system.out.println ("1. The distance between two points is calculated by the spherical distance is" +distance1); System.out.println ("2. Pythagorean theorem calculates the distance between two points is" +distance2), }//calculates the direct distance of two points according to the spherical distance public static double Getlongdistance (double lon1,&NBSP;DOUBLE&NBSP;LAT1,&NBSP;DOUBLE&NBSP;LON2,&NBSP;DOUBLE&NBSP;LAT2) { double ew1, ns1, ew2, ns2; double distance; // angle conversion to radians ew1 = lon1 * def_pi180; ns1 = lat1 * def_pi180; ew2 = lon2 * def_pi180; ns2 = lat2 * def_pi180; // Find the angle (radian) distance = math.sin (ns1) * math.sin (NS2) + math.cos (counterclockwise) of the great circle and the spherical sphere. NS1) * math.cos (NS2) * math.cos (EW1&NBSP;-&NBSP;EW2); // adjusted to [ -1 1] within , avoid overflow if (distance > 1.0) distance = 1.0; else if (distance < -1.0) distance = -1.0; // Large Circle Counterclockwise length distance = def_r * math.acos (distance); return distance;} Calculates the distance between two points according to the Pythagorean Theorem public static double getshortdistance (DOUBLE&NBSP;LON1,&NBSP;DOUBLE&NBSP;LAT1, &NBSP;DOUBLE&NBSP;LON2,&NBSP;DOUBLE&NBSP;LAT2) { double ew1, ns1, ew2, ns2; double dx, dy, dew; double distance; // Angle converted to radians ew1 = lon1 * DEF_PI180; ns1 = lat1 * DEF_PI180; ew2 = lon2 * DEF_PI180; ns2 = lat2 * DEF_PI180; // Longitude difference dew = ew1 - ew2; // 180 degrees across east and west, Make adjustments if (DEW&NBSP;>&NBSP;DEF_PI) dew = DEF_2PI - dew; else if (DEW&NBSP;<&NBSP;-DEF_PI) dew = DEF_2PI + dew; dx = def_r * math.cos (NS1) * dew; // East-west length (cast on the latitude circleShadow length) dy = DEF_R * (NS1&NBSP;-&NBSP;NS2); // North-south direction length (projection length on the longitude circle) // Pythagorean distance = math.sqrt (dx * dx + dy * DY); return distance;}}
The Code implementation section itself can be encapsulated into a tool class, and then passed to calculate the latitude and longitude of two points, you can get two points distance.
You see the beginning of the test result is a double type of data after the decimal point has a very long decimal, the actual use we may not need to be so accurate, generally to two decimal places, according to the following method, the distance value assigned to the TextView display can be:
Handle data of type double to two digits after decimal point double temp = gettwopointdistanceutils.getlongdistance (MLON1,MLAT1, lon, LAT)/1000; BigDecimal bd = new BigDecimal (temp);d ouble distance = Bd.setscale (2,bigdecimal.round_half_up). Doublevalue ();
Finally see my final implementation effect:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/74/48/wKiom1YX2OzRb7vDAAbN-prT6DE211.jpg "title=" 1.png " alt= "Wkiom1yx2ozrb7vdaabn-prt6de211.jpg"/>
This article is from the "Sunnygeek Technology blog" blog, please be sure to keep this source http://sunnygeek.blog.51cto.com/9485654/1701370
How to calculate Android two point distance