Topic: Silly math...move a marker at fixed speed towards another marker
20 years ago this would have been trivial for me, but I can't wrap my head around what I think is a pretty easy scenario:
I have two types of markers: city markers, and hireling markers. Hireling markers can move (from city to city, or to other locations), and their position is controlled directly by the server, with periodic updates sent down to the client.
This means that every time an update comes down from the server, the hireling marker "leaps" to the new position (long/lat coords).
On the client, I would like to give the hireling marker a small velocity towards the city marker it is moving to, so that I don't have to send so many updates yet still have the marker move more smoothly.
Obviously, markers don't have underlying physics, so I'm more than willing to use observe() to update the marker's position every few seconds to simulate smooth movement...but that's where I'm stuck.
The examples (animating a marker, smooth move of the camera to a marker) all use lerp, but unfortunately don't seem to indicate how to control the velocity of the marker directly with a rate (for example, 5 km/hour). Additionally, I strongly suspect that lerp will fail across lat/lng sign changes (equator, prime meridian), sending my marker off into semi-random space.
On my server, my geocoder implementation has a utility called endpoint, which does the following (the language is Ruby, but should be relatively easy to understand):
# Given a start point, heading (in degrees), and distance, provides
# an endpoint.
def endpoint(start, heading, distance, options = {})
options[:units] ||= Geocoder.config.units
radius = earth_radius(options[:units])
start = extract_coordinates(start)
# convert degrees to radians
start = to_radians(start)
lat = start[0]
lon = start[1]
heading = to_radians(heading)
distance = distance.to_f
end_lat = Math.asin(Math.sin(lat)*Math.cos(distance/radius) +
Math.cos(lat)*Math.sin(distance/radius)*Math.cos(heading))
end_lon = lon+Math.atan2(Math.sin(heading)*Math.sin(distance/radius)*Math.cos(lat),
Math.cos(distance/radius)-Math.sin(lat)*Math.sin(end_lat))
to_degrees [end_lat, end_lon]
end
Is there anything similar in OnlineMaps? I've searched through the OnlineMapsUtils.cs and some of the pieces seem to be there, but I can't quite figure out how to get it working together.
Any ideas or suggestions?
Thanks in advance...