i'm making app where i using GPS and counting traveled distance, i'm downloading current loaction and counting distance between two location. When i'm trying to get current location it is showing me different location (i don't move my phone) and when i'm trying to count distance i'm geting wrong result :/ here source code
GPSTracker:
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
public static double distance=0;
Location location1,location2;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Activity activity;
Location location;
double latitude;
double longitude;
private static final float MIN_DISTANCE_CHANGE_FOR_UPDATES = 50;
private static final long MIN_TIME_BW_UPDATES = 1000 * 10;
protected LocationManager locationManager;
public GPSTracker(Context context, Activity activity) {
this.mContext = context;
this.activity = activity;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
int requestPermissionsCode = 50;
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, requestPermissionsCode);
} else {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
if(isGPSEnabled) {
if(location == null) {
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50);
} else {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}
} catch (Exception e){
e.printStackTrace();
}
return location;
}
private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
if(location1 == null){
location1 = location;
} else {
location2 = location1;
location1 = location;
distance += location1.distanceTo(location2) / 1000f;
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
DistanceActivity:
public class DistanceActivity extends Activity{
Button startB, stopB;
TextView textView;
GPSTracker gps;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distance);
startB = findViewById(R.id.start_B);
stopB = findViewById(R.id.stop_B);
textView = findViewById(R.id.distance_TV);
mContext = this;
startB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gps = new GPSTracker(mContext,DistanceActivity.this);
if(gps.canGetLocation()){
textView.setText("Distance: "+String.format("%.2f Km",gps.distance ));
} else {
textView.setText("Cannot count distance.");
}
}
});
}
}