0

I have a GoogleMap in my project. It's set in zoom level 21. I want to draw a line that is 5 meter in length with a specific heading. I used this code:

private LatLng drawHeadingOnMap(LatLng centre, double radius, double heading) 
{ 
double EARTH_RADIUS = 6378100.0; 
// Convert to radians. 

double lat = Math.toRadians(centre.latitude );
double lon = Math.toRadians(centre.longitude);

// y 
double latPoint = lat + (radius / EARTH_RADIUS) * Math.sin(Math.toRadians(heading)); 
// x 
double lonPoint = lon + (radius / EARTH_RADIUS) * Math.cos( Math.sin(Math.toRadians(heading)) / Math.cos(lat));

LatLng point =new LatLng(latPoint * 180.0 / Math.PI, lonPoint * 180.0 / Math.PI);
return point; 
}

I run it by:

LatLng ll = drawHeadingOnMap(origin, 5, 90);
LatLng lll = drawHeadingOnMap(origin, 5, 0);

googleMap.addPolyline(new PolylineOptions().add(Mabda).add(ll).color(Color.BLUE).width(3));
googleMap.addPolyline(new PolylineOptions().add(Mabda).add(lll).color(Color.BLUE).width(3));

It draw 0 degree very well!! but others are wrong. for example this pic is shown the above code : enter image description here

When I want to draw 90 degree, It draw sth like this pic! and after 90 , it get back to 0 degree (When I write drawHeadingOnMap(origin, 5, 180), It draw 0 degree!). How can I fix it? I'm so confused !!!...

Updated: I tried it for origin= (12,12)... I got this result:

ll.Latitude = 12.000898320495335
ll.Longitude = 12.00046835742835
lll.latitude = 12.0
lll.longitude = 12.000898320495335

ll is result for moving of (12,12) for 1 meter in direction of 90 degree. lll is result for moving of (12,12) for 1 meter in direction of 0 degree. the method is just OK for 0 degree ...

Community
  • 1
  • 1
jion
  • 127
  • 13

2 Answers2

1

If you have a center point (10, 20), and you want to find the other point (x, y) to its 20 degree with radius 5, you can do the following math:

x = 10 + 5 *  Math.sin(Math.toRadians(20));
y = 20 + 5 *  Math.cos(Math.toRadians(20));  

Not sure why you did Math.cos( Math.sin(Math.toRadians(heading)) / Math.cos(lat)) for your lonPoint.

ztan
  • 6,861
  • 2
  • 24
  • 44
  • I take that code from http://stackoverflow.com/questions/18650907/latlng-points-on-circle-on-goggle-map-v2-in-android..... and I had a mistake... thanks a lot for your mention.I corrected it : double lonPoint = lon + (radius / EARTH_RADIUS) * Math.cos( Math.toRadians(heading) / Math.cos(lat)); now 0 and 180 degree shown well! but others are wrong :| – jion Feb 05 '15 at 07:23
0

To understand exact math I suggest reading this link.

If a working implementation is all you need use this function (adopted from Maps SphericalUtil):

/**
 * @param loc location to transale (creates a copy)
 * @param distance in meters
 * @param heading in degrees, where 0 is NORTH, clockwise
 * @return new location
 */
public static LatLng translate(LatLng loc, double distance, double heading){
    double EARTH_RADIUS = 6378100.0;
    heading = Math.toRadians(heading);
    distance = distance/EARTH_RADIUS;
    // http://williams.best.vwh.net/avform.htm#LL
    double fromLat = Math.toRadians(loc.latitude);
    double fromLng = Math.toRadians(loc.longitude);
    double cosDistance = Math.cos(distance);
    double sinDistance = Math.sin(distance);
    double sinFromLat = Math.sin(fromLat);
    double cosFromLat = Math.cos(fromLat);
    double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * Math.cos(heading);
    double dLng = Math.atan2(
            sinDistance * cosFromLat * Math.sin(heading),
            cosDistance - sinFromLat * sinLat);
    return new LatLng(Math.toDegrees(Math.asin(sinLat)), Math.toDegrees(fromLng + dLng));
}
gcasar
  • 729
  • 1
  • 9
  • 22