For my application I have to find the position of a point on Google map knowing only that it's located between 2 other points and the time (in ms) when the coordinates have been caught.
In my code, assumed A and B as the points given and X as the point to find, I:
calculate distance between A and B
basing on time I found out the speed (in micro degrees /ms) to travel from A to B
I found the distance from point A and point X (using time and speed)
using similar triangle's rule, I calculate latitude and longitude of point X from point A
This workflow bring out errors on the map, so, often the X marker is not on the line between A and B markers.
How can I make it works better? Is it a problem with the sphericity of the globe?
Thank you to all.
Here is the code:
int ax = oldPoint.getLatitude();
int ay = oldPoint.getLongitude();
int bx = currentPoint.getLatitude();
int by = currentPoint.getLongitude();
long at = oldPoint.getDataRilevamento(); //get time first point
long bt = currentPoint.getDataRilevamento(); // get time second point
long xt = x.getDate(); // time of point to find
int c1 = bx-ax;
int c2 = by-ay;
double hyp = Math.sqrt(Math.pow(c1, 2) + Math.pow(c2, 2));
double vel = hyp / (bt-at);
double pos = vel*(xt - at);
int posx = (int)((pos*c1)/hyp);
int posy = (int)((pos*c2)/hyp);
x.setLatitude(ax+posx); //set the latitude of X
x.setLongitude(ay+posy); // set the longitude of X
Your problem can be solved by taking the following steps.
Calculate the distance from points A and B (use the Haversine formula, which is good enough here, or the more complicated Vincenty formula). To use the formula correctly, Android's microdegrees, which is what getLatitude and getLongitude return, must be converted to radians, using a formula like this:
Calculate the bearing (direction) from points A and B (use the formula on the same page). This will be different from the Pythagorean formula because the earth is round, not flat.
Convert the radians from the generated point into microdegrees using this formula:
Putting it all together, we have the following function, which I place in the public domain:
And here's how it's used: