Android gain latitude and longitude calculation distance introduction _android

Source: Internet
Author: User
Tags asin cos pow sin stub

Longitude indicates north-south direction, longitudinal
Latitude indicating the direction of things, crosswise

Get latitude and longitude degree

Using GPS Permissions:

Copy Code code as follows:

<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION" ></uses-permission>

Android provides Locationmanager and location for easy access to latitude and longitude, elevation and other locations. Use the Locationmanager to obtain the position management class, thus obtains the historical GPS information as well as the position change listens for registers, uses the location to obtain the concrete location information. The code is as follows:

Copy Code code as follows:

Locationm = (Locationmanager) getsystemservice (Location_service);
Criteria criteria = new criteria ();
Criteria.setaccuracy (Criteria.accuracy_fine);
Criteria.setaltituderequired (FALSE);
Criteria.setbearingrequired (FALSE);
Criteria.setcostallowed (TRUE);
Criteria.setpowerrequirement (Criteria.power_low);
String Provider = Locationm.getbestprovider (criteria, true);

Location Location = locationm.getlastknownlocation (provider);
Get the last record
Gps_loc (location);

Locationlistener Gps_listener = new Locationlistener () {
Monitor position change and get location information in real time
@Override
public void onstatuschanged (String provider, int status,
Bundle Extras) {
TODO auto-generated Method Stub

}

@Override
public void onproviderenabled (String provider) {
TODO auto-generated Method Stub

}

@Override
public void onproviderdisabled (String provider) {
TODO auto-generated Method Stub

}

@Override
public void onlocationchanged (Location Location) {
TODO auto-generated Method Stub
When the position changes
Gps_loc (location);
}
};
Locationm.requestlocationupdates (provider, 1000, 0, Gps_listener);
}

Get your own place
private void Gps_loc (Location Location) {
if (location!= null) {
Self_weidu = Location.getlatitude ();
Self_jindu = Location.getlongitude ();
} else {
Self_weidu = 0;
Self_jindu = 0;
}
}

Two point latitude and longitude, calculate the distance

1.lat1 Lung1 represents the latitude and longitude of a point, Lat2 Lung2 the latitude and longitude of B point;

The difference between the latitude of two points and the b=lung1-lung2 of longitude of two points is 2.a=lat1–lat2;

3.6378.137 is the earth radius, the unit is kilometer;

The calculated result unit is a kilometer.

Take a piece of code directly from Google Maps's script.
Maps code: The result of the calculation is meters.

Copy Code code as follows:

Calculate two point distance
Private final Double Earth_radius = 6378137.0;
Private double gps2m (double lat_a, double lng_a, double lat_b, double lng_b) {
Double RADLAT1 = (lat_a * math.pi/180.0);
Double RADLAT2 = (Lat_b * math.pi/180.0);
Double A = RADLAT1-RADLAT2;
Double b = (lng_a-lng_b) * math.pi/180.0;
Double s = 2 * Math.asin (MATH.SQRT (Math.pow) (Math.sin (A/2), 2)
+ Math.Cos (RADLAT1) * Math.Cos (RADLAT2)
* MATH.POW (Math.sin (B/2), 2));
s = S * earth_radius;
s = Math.Round (S * 10000)/10000;
return s;
}

Two-point latitude and longitude, calculation of azimuth angle

Calculate Azimuth PAB

Where Lat_a, Lng_a is the latitude and longitude of a, lat_b, Lng_b is the latitude and longitude of B. The code is as follows:

Copy Code code as follows:

Calculate the azimuth PAB.
Private double gps2d (double lat_a, double lng_a, double lat_b, double lng_b) {
Double d = 0;
lat_a=lat_a*math.pi/180;
lng_a=lng_a*math.pi/180;
lat_b=lat_b*math.pi/180;
lng_b=lng_b*math.pi/180;

D=math.sin (lat_a) *math.sin (lat_b) +math.cos (lat_a) *math.cos (lat_b) *math.cos (lng_b-lng_a);
D=MATH.SQRT (1-D*D);
D=math.cos (Lat_b) *math.sin (lng_b-lng_a)/D;
D=math.asin (d) *180/math.pi;
D = Math.Round (d*10000);
return D;
}

Related Article

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.