Reputation: 1324
I am working on an ITS(Intelligent Transport System). So I have to calculate the EAT(Estimated Arrival Time) at the bus stop for a bus. Now I have the two points(lat, lng) of stop and position of vehicle at present. So I have to calculate distance. next I ll get speed SO I can calculate the Arrival Time at the stop for bus. My problem is, I am using this method to calculate distance between two points,
private 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);
}
What I am thinking is, it calculates the straight line distance between two points. It wont happen in real time. because a vehicle travel by road. So is there any better solution or algorithm to find out the distance between two places. Please any one help me in this.
Upvotes: 1
Views: 1396
Reputation: 59
You can use the LatLangTool method below
public static Double getDistanceToLocation(SourceCordinate from, SourceCordinate to) {
LatLng busDeparturePoint = new LatLng(from.latitude, from.longitude);
LatLng busDestinationPoint = new LatLng(to.latitude, to.longitude);
return LatLngTool.distance(busDeparturePoint, busDestinationPoint, LengthUnit.KILOMETER);
}
Upvotes: 0
Reputation: 13467
The distance depends on the bus route.
If your Intelligent Transport System knows the bus route, then you can use that info.
Does Google Maps have these bus routes? You can use the Google Maps API to ask it for transit routes from the origin point to the destination point, but you'll have to determine which of the choices are right, if any.
If your actual goal is to determine the ETA, then what you need is the transit schedule, not the distance. (The passenger needs to wait for the bus to arrive, the bus may wait at certain stops to meet up with other lines, and so on.) The Google Maps API can help with this as long as it knows about the relevant bus lines.
Upvotes: 2
Reputation: 771
You are right, calculating the direct distance of two geographical position will not be enough as routes are often not straight. Google map api can help you in this case.
You can use the google map Direction Service. Checkout the following Transit mode:
https://developers.google.com/maps/documentation/javascript/directions#TravelModes
Offcourse, you have to take in consideration the stop time and average speed of the bus. Since bus will not always have the same speed, you need to calculate the average speed, keep traffic lights and other related things into consideration (may be traffic jam even?). It will be best, if your estimated time also shows possible delays and update it in real time (when bus will be in traffic jam, in can add the waiting time with delay time).
Upvotes: 2