I get the GPS location 3 way like Map.getMyLocation().getLatitude()
, locationManager
and mGoogleApiClient
.
but these are not helped for me for calculating distance every 15 sec.
problem is location latitude and longitude are not accurately i.e I am walking like 1-2 meter with using these way distance calculation. calculating distance is 400 m-600 meter sometimes show 1 km also. GPS location variate like 20 meters to 600 meters. Please give some better idea about how to get my location heavy accuracy with calculating distance every 15 sec in android.
this is my code
//get current location
try {
locationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
old_latitude = location.getLatitude();
old_longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
old_latitude = location.getLatitude();
old_longitude = location.getLongitude();
Log.d("GPS Enabled", "GPS Enabled");
}
}
}
}
}
// Toast.makeText(this, "Current location"+current_latitude+","+current_longitude , Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
/* GPSTracker gpsTracker = new GPSTracker(Activity_Map_Advance_Tracking.this);
if (gpsTracker.getIsGPSTrackingEnabled()) {
old_latitude=gpsTracker.latitude;
old_longitude=gpsTracker.longitude;
}*/
old_latitude = mMap.getMyLocation().getLatitude();
old_longitude = mMap.getMyLocation().getLongitude();
if (trip_status.equals("trip"))
ha.postDelayed(new Runnable() {
@Override
public void run() {
//get current location
try {
locationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
new_latitude = location.getLatitude();
new_longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
new_latitude = location.getLatitude();
new_longitude = location.getLongitude();
Log.d("GPS Enabled", "GPS Enabled");
}
}
}
}
}
// Toast.makeText(this, "Current location"+current_latitude+","+current_longitude , Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
// check if GPS enabled
/* GPSTracker gpsTracker = new GPSTracker(Activity_Map_Advance_Tracking.this);
if (gpsTracker.getIsGPSTrackingEnabled()) {
new_latitude = gpsTracker.latitude;
new_longitude =gpsTracker.longitude;
}*/
new_latitude = mMap.getMyLocation().getLatitude();
new_longitude = mMap.getMyLocation().getLongitude();
double GPS_jump = 0.000100;
if (Math.abs(old_latitude - new_latitude) < GPS_jump && Math.abs(old_longitude - new_longitude) < GPS_jump) {
waiting_sec = waiting_sec + 15;
if (waiting_sec >= (waiting_time * 60)) {
if (wait == 0) {
waitng_charge = (waiting_amnt_per_sec * waiting_sec) + waitng_charge;
wait = 15;
//waiting_sec=0;
} else {
waitng_charge = (waiting_amnt_per_sec * wait) + waitng_charge;
}
}
// total_distance=meterDistanceBetweenPoints(13.020876, 80.222541,13.021301, 80.222881)+total_distance;
// client_info.setText("TOTAL DISTANCE:"+total_distance/1000);
System.out.println("vehicle not move" + Math.abs((float) old_latitude - (float) new_latitude) + "," + Math.abs((float) old_longitude - (float) new_longitude));
Toast.makeText(Activity_Map_Advance_Tracking.this, "vehicle not move" + Math.abs((float) old_latitude - (float) new_latitude) + "," + Math.abs((float) old_longitude - (float) new_longitude), Toast.LENGTH_LONG).show();
// assuming doubles are equals
} else {
System.out.println("vehicle moved" + Math.abs((float) old_latitude - (float) new_latitude) + "," + Math.abs((float) old_longitude - (float) new_longitude));
if ((waiting_sec) >= (waiting_time * 60)) {
waitng_charge = waiting_amnt_per_sec + waitng_charge;
}
total_distance = meterDistanceBetweenPoints(old_latitude, old_longitude, new_latitude, new_longitude) + total_distance;
waiting_sec = 0;
wait = 0;
Toast.makeText(Activity_Map_Advance_Tracking.this, "vehicle moved" + Math.abs((float) old_latitude - (float) new_latitude) + "," + Math.abs((float) old_longitude - (float) new_longitude), Toast.LENGTH_LONG).show();
Toast.makeText(Activity_Map_Advance_Tracking.this, "****TOTAL DISTANCE*****->" + total_distance, Toast.LENGTH_LONG).show();
client_info.setText("TOTAL DISTANCE:" + total_distance);
}
delay_sec = delay_sec + 15;
System.out.println("After " + delay_sec + " sec \n waiting sec" + waiting_sec + " sec\n waitng charge" + waitng_charge + "rs \n currunt lat and lng" + new_latitude + " , " + new_longitude + "" +
"\n___old lat lng" + old_latitude + " , " + old_longitude);
Toast.makeText(Activity_Map_Advance_Tracking.this, "After " + delay_sec + " sec \n waiting sec" + waiting_sec + " sec\n waitng charge" + waitng_charge + "rs \n currunt lat and lng" + new_latitude + " , " + new_longitude + "" +
"\n___old lat lng" + old_latitude + " , " + old_longitude + ", \ntotal distance: " + total_distance, Toast.LENGTH_LONG).show();
old_latitude = new_latitude;
old_longitude = new_longitude;
//call function
ha.postDelayed(this, 15000);
}
}, 15000);
this is my distance calculation method but this method is not issue
private float meterDistanceBetweenPoints(double lat_a, double lng_a, double lat_b, double lng_b) {
/*Location locationA = new Location("point A");
locationA.setLatitude(lat_a);
locationA.setLongitude(lng_a);
Location locationB = new Location("point B");
locationB.setLatitude(lat_b);
locationB.setLongitude(lng_b);
float distance = locationA.distanceTo(locationB);*/
double earthRadius = 6371000; //meters
double dLat = Math.toRadians(lat_b - lat_a);
double dLng = Math.toRadians(lng_b - lng_a);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat_a)) * Math.cos(Math.toRadians(lat_b)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
float dist = (float) (earthRadius * c);
System.out.println("**********this is distance calculation**********" + dist);
return dist;
}