I have two points A and B with their respective latitude and longitude. I also have a time it takes to get from point A to point B. Let say it takes 1 hour and from A assume the driver goes straight to point B and mantains a same velocity during the trip. After 0.6 hour, I would like to know the current location of the driver (by latitude and longitude). Is there any function in the package geosphere
or any other packages that allow me to do so? Thanks
-
TDo: See interpretation I used in my answer, which gets you back into the realm of a programming question. But I agree with the question being put on hold as you didn't provide any evidence of having tried this for yourself. – Andy Clifton Oct 23 '16 at 20:31
1 Answers
I think your question is better and more succinctly phrased as "how do I find the great circle route between two locations (defined as lat/long coordinates), and then find the coordinates of the point at an arbitrary percentage of the way along that route?".
First let's make an arbitrary pair of locations, called a and b:
df <- data.frame(locations = c("a","b"),
lon =runif(2,min = -180, max = 180),
lat = runif(2,min = -90, max = 90))
Now, we look at the great circle route between them. We don't need the route itself, just the distance of the whole route, and the initial direction.
require(geosphere)
# get the distance of a great circle route between these points
track.dist = distHaversine(p1 = df[1,c("lon","lat")],
p2 = df[2,c("lon","lat")])
And then get the initial bearing, which we'll use in a bit:
track.init.bearing = bearing(p1 = df[1,c("lon","lat")],
p2 = df[2,c("lon","lat")])
The next step is to figure out where we are at arbitrary fraction of the elapsed route:
# figure out where we are at an arbitrary time
current.location.fraction = runif(1,min = 0, max = 1)
# get the distance
current.location.dist = current.location.fraction * track.dist
current.location = as.data.frame(destPoint(p = df[1,c("lon","lat")],
b = track.init.bearing,
d = current.location.dist))
And the last step is to check that we are the right fraction of the distance along the route:
check.dist = distHaversine(p1 = df[1,c("lon","lat")],
p2 = c(current.location$lon,
current.location$lat))
print(current.location.fraction)
print(check.dist / track.dist)
In my tests, these last two numbers were usually within 1% of each other, suggesting this isn't too bad.
So, you can just pull the results from the current.location
data frame.

- 4,926
- 3
- 35
- 47