I have developed an app like uber but I am facing issue in calculating correct fare due to incorrect distance from pick up location to the location where trip completed. What I have done and doing:
1st approach
I was getting distance between between current location of the driver(save location when trip started) and end location(where trip completed) like this.
distanceInMeters = locationChanged.getmCurrentlocation().distanceTo(loc_pick_up);
but problem with this code is. if passenger pick up location(where trip started) and drop off location(where trip completed) is same distance is zero.
2nd Approach
Now I am calling a method every minute. which when executed first time. calculate distance between pickup location(from where trip started) and current location(location after 1 minute) and save difference in a variable. and than current location becomes pickup location and when method is executed again. it calculates distance between pickuplocation and current location (between location before and after 1 minutes) and add the result in already saved distance.
private void locationDistanceHandler() {
handlerLocation = new Handler();
runnableCode = new Runnable() {
@Override
public void run() {
Location loc_pick_up = new Location("");
if (!pick_up_value_used) {
loc_pick_up.setLatitude(pickUpLatLng.latitude);
loc_pick_up.setLongitude(pickUpLatLng.longitude);
locationChanged.setmPickUpLocation(locationChanged.getmCurrentlocation());
pick_up_value_used = true;
} else {
loc_pick_up.setLatitude(locationChanged.getmPickUpLocation().getLatitude());
loc_pick_up.setLongitude(locationChanged.getmPickUpLocation().getLongitude());
}
Log.d(Constants.LOG, "handle location called:" + distanceInMeters);
distanceInMeters = distanceInMeters + locationChanged.getmCurrentlocation().distanceTo(loc_pick_up);
handlerLocation.postDelayed(this, 60000);
}
};
handlerLocation.postDelayed(runnableCode, 60000);
}
But this is not accurate. today I travelled 20 km from office to home in 18 minutes. but this method calculated distance 6.8km.
Please suggest me a better way of doing this or any working source code example. so I can resolve this issue.
float[] results=new float[5];
loc_pick_up.distanceBetween(startLatitude,startLongitude, endLatitude, endLongitude,results);
String calculatedDistance=results[0];
System.out.println("Distance between point A and point B are: " + calulcatedDistance);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.