SoFunction
Updated on 2025-03-11

Introduction to obtaining latitude and longitude calculation distance on Android

Longitude indicates the north-south direction, longitudinal
Latitude indicates east-west direction, horizontal direction

Obtain latitude and longitude

Use GPS permissions:

Copy the codeThe code is as follows:

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

Android provides LocationManager and Location, which can easily obtain latitude, longitude, altitude and other locations. Use LocationManager to obtain location management classes, so that historical GPS information and listening registration for location changes can be obtained; use Location to obtain specific location information. The code is as follows:

Copy the codeThe code is as follows:

locationm = (LocationManager) getSystemService(LOCATION_SERVICE);
   Criteria criteria = new Criteria();
   (Criteria.ACCURACY_FINE);
   (false);
   (false);
   (true);
   (Criteria.POWER_LOW);
   String provider = (criteria, true);

   Location location = (provider);
//Get the last record
   gps_loc(location);

   LocationListener GPS_listener = new LocationListener() {
//Check the location changes and obtain the 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);
}
};
(provider, 1000, 0, GPS_listener);
}

// Get your own position
private void gps_loc(Location location) {
   if (location != null) {
       self_weidu = ();
       self_jindu = ();
   } else {
       self_weidu = 0;
       self_jindu = 0;
   }
}

Two points latitude and longitude, calculate distance

1.Lat1 Lung1 represents the latitude and longitude of point A, and Lat2 Lung2 represents the latitude and longitude of point B;

=Lat1 – Lat2 is the difference between the latitudes of two points b=Lung1 -Lung2 is the difference between the longitudes of two points;

3.6378.137 is the radius of the earth, with units of kilometers;

The calculated result unit is kilometers.

I just cut out the code from the Google maps script.
The code of maps: The result of the calculation is in meters.

Copy the codeThe code is as follows:

// Calculate the distance between two points
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 * / 180.0);
double radLat2 = (lat_b * / 180.0);
double a = radLat1 - radLat2;
double b = (lng_a - lng_b) * / 180.0;
double s = 2 * ((((a / 2), 2)
+ (radLat1) * (radLat2)
* ((b / 2), 2)));
s = s * EARTH_RADIUS;
s = (s * 10000) / 10000;
return s;
}

Two points longitude and latitude, 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 the codeThe code is as follows:

// Calculate the azimuth angle pab.
private double gps2d(double lat_a, double lng_a, double lat_b, double lng_b) {
double d = 0;
lat_a=lat_a*/180;
lng_a=lng_a*/180;
lat_b=lat_b*/180;
lng_b=lng_b*/180;

d=(lat_a)*(lat_b)+(lat_a)*(lat_b)*(lng_b-lng_a);
   d=(1-d*d);
   d=(lat_b)*(lng_b-lng_a)/d;
   d=(d)*180/;
   // d = (d*10000);
return d;
}