Здравствуйте, я не знаю, поможет ли это, но однажды я написал класс, в котором вычислял его вручную, вводя параметры в соответствующие методы:
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Looper;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import java.util.List;
/**
* Created by Ibkunle Adeoluwa on 1/8/2019.
*/
public class LocationManager {
private FusedLocationProviderClient fusedLocationProviderClient;
private LocationCallback locationCallback;
private LocationRequest locationRequest;
//TODO Import these Libraries in gradle
/*
implementation 'com.google.android.gms:play-services-location:11.8.0'
implementation 'com.karumi:dexter:5.0.0'
*/
private Activity myActivity;
public LocationManager(Activity myActivity) {
this.myActivity = myActivity;
}
public void requestLocationPermissions() {
//Request required permissions using Dexter Library ACCESS_COARSE_LOCATION & ACCESS_FINE_LOCATION
Dexter.withActivity(myActivity)
.withPermissions(Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
if (report.areAllPermissionsGranted()) {
buildLocationRequest();
buildLocationCallback();
if (ActivityCompat.checkSelfPermission(myActivity, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(myActivity, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(myActivity);
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
Toast.makeText(myActivity, "Location Permission Denied", Toast.LENGTH_SHORT).show();
}
}).check();
}
private void buildLocationRequest() {
//Get Location
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setSmallestDisplacement(10.0f);
}
private Location buildLocationCallback() {
//Process location response
final Location location = new Location();
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
//Log
Log.d("Location", locationResult.getLastLocation().getLatitude()
+ "/" + locationResult.getLastLocation().getLongitude());
location.setLatitude(locationResult.getLastLocation().getLatitude());
location.setLongitude(locationResult.getLastLocation().getLongitude());
}
};
return location;
}
//TODO How to Use
/***
* Sample Request
*
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'M') + " Miles\n");
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'K') + " Kilometers\n");
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'N') + " Nautical Miles\n");
System.out.println(distance(10.46786, -98.53506,32.9697, -96.80322, 'M') + " Initial Miles\n");
System.out.println(distance(32.9697, -96.80322, 29.46786, -98.53506, 'M') + " Covered Miles\n");
System.out.println(coveredDistance(distance(10.46786, -98.53506,32.9697, -96.80322), distance(32.9697, -96.80322, 29.46786, -98.53506))+ " Left Distance\n");
System.out.println(percentageCoveredDistance(262.6777938054349,1558.5453389875424)+ " Covered Distance Percentage \n");
*
*
***/
/**
* Expected Output
*
262.6777938054349 Miles
422.73893139401383 Kilometers
228.10939614063963 Nautical Miles
1558.5453389875424 Initial Miles
262.6777938054349 Covered Miles
1295.8675451821075 Left Distance
17% Covered Distance Percentage
*
*
**/
/**
*
* @param lat1
* @param lon1
* @param lat2
* @param lon2
* @param unit
* @return
*/
private static double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
/**
*
* @param lat1
* @param lon1
* @param lat2
* @param lon2
* @return
*/
private static double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/**
*
* @param deg
* @return
*/
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/**
*
* @param rad
* @return
*/
private static double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts subtracts initial from current distance:*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/**
*
* @param initialDistance
* @param currentDistance
* @return
*/
private static double coveredDistance(double initialDistance, double currentDistance) {
return (initialDistance - currentDistance);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::This function converts the covered distance to percentage of the total :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/**
*
* @param coveredDistance
* @param totalDistance
* @return
*/
private static String percentageCoveredDistance(double coveredDistance, double totalDistance) {
double percent = (100 * coveredDistance) / totalDistance;
return String.format("%.0f%%", percent);
}
}