Moving a point along google map Polyline

by TSR   Last Updated September 11, 2019 18:20 PM - source

My goal to simulate the motion of a car on a given road. To do that, I use google map direction API to fetch the direction polyline (an array of LatLng)

Now every second, I calculate the new position of the car based on its speed and its previous position.

And the cycle repeats every second.

To simplify my problem, let's say that the car is moving along a straight road with a certain velocity.

The google direction API will give me an array of only two LatLng, A and B (The start point and finish point since the road is straight)

Based on the velocity of the car and delta time, I calculated the car has moved away from point A towards B by a distance D in meters.

If I know the coordinates of A, B and the value of D, how do I calculate the new position of the car?


My attempt assumes that the Earth is Flat and the LatLng A and B are cartesian coordinates in 2D planes. So I just used the cartesian rule taken from here Finding a point along a line a certain distance away from another point!

enter image description here

* A to B is a straight line on the Map
* A is the origin
* B is the destination
* d is the distance offset from A
function getNewPosition(A, B, d){
   const t = d / distanceBetweenTwoLatLng(A, B)
   const lat = (1-t)**A.lng
   const lng = (1-t)*B.lng+t*B.lng
   return {lat, lng}

But obviously, the earth is NOT flat. Furthermore, I am not sure if I can just assume that LatLng could be assumed as cartesian points directly

Related Questions

Spherical coordinates: initial bearing to intercept

Updated March 05, 2017 20:20 PM