0

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;
}
Jorgel
  • 920
  • 3
  • 14
  • 28
  • 1
    Can you provide a link to the doc what Auxiliar does? Keep in mind that latitude and longitude are angels – Mirco Jul 17 '14 at 14:08
  • I used the Haversine formula to calculate the distance, as answered here http://stackoverflow.com/questions/120283/how-can-i-measure-distance-and-create-a-bounding-box-based-on-two-latitudelongi/123305#123305 I will edit my question with the code – Jorgel Jul 17 '14 at 14:23
  • [example (using the Google Maps Javascript API v3, v2 was deprecated and turned off quite a while ago) animating a marker along a polyline](http://www.geocodezip.com/v3_animate_marker_xml.html) – geocodezip Jul 18 '14 at 19:49

0 Answers0