I need to move one marker from one point to another in a Google Maps application. However, I am having trouble working with LatLng coordinates, especially because of precision errors.
I tried the basic for 2d movement, but it seems it's not the correct approach with earth coordinates
double dx = target.longitude - position.longitude;
double dy = target.latitude - position.latitude;
//Auxiliar.distance returns the distance in meters between two coordinates
double lenght = Auxiliar.distance(target, position);
dx *= SPEED/lenght;
dy *= SPEED/lenght;
position = new LatLng(position.latitude + dy,
position.longitude + dx);
I also got precision errors when trying to subtract the coordinates, since they are doubles, so I tried to use BigDecimal, but still with no success.
Is there a algorithm to do a straight line movement in earth coordinates?
Here is the code for my distance function
public static double distance(LatLng point1, LatLng point2)
{
double earthRadius = 6371 ;
double dLat = Math.toRadians(point2.latitude-point1.latitude);
double dLng = Math.toRadians(point2.longitude-point1.longitude);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
* Math.cos(Math.toRadians(point1.latitude)) * Math.cos(Math.toRadians(point2.latitude));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
return dist*1000;
}