Android gets an introduction to latitude and longitude calculations

  • 2020-05-24 06:06:10
  • OfStack

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

Get latitude and longitude

Using GPS permissions:


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

Android offers LocationManager and Location for easy access to latitude, longitude and altitude. Use LocationManager to get the location management class, so you can get the historical GPS information and the monitoring registration of location changes. Use Location to get specific location information. The code is 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 the position change to obtain the position 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 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 latitude and longitude, calculate the distance

1.Lat1, Lung1, A, Lat2, Lung2, B;

2.a=Lat1, Lat2 is the difference of latitude between two points. b=Lung1 -Lung2 is the difference of longitude between two points.

3.6378.137 is the radius of the earth in kilometers;

The result is calculated in kilometers.

Ripped a piece of code directly from the google maps script.
Code for maps: the calculated result is in meters.


//  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 * 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 latitude and longitude, calculate the azimuth

Calculate the azimuth pab

Where lat_a and lng_a are the latitude and longitude of A; lat_b, lng_b are the latitude and longitude of B. The code is 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 articles: