latitude longitude - Calculate distance in (x, y) between two GPS-Points -
i'm looking smooth way calculate distance between 2 gps points, result like: "you have go x meters , y meters left - can work 2d-coordinate system, have position (0,0) , other positions showing distance in (x, y) in meters position.
my idea calculate distance between points using haversine formula. (this returns hypotenuse)
in addition that, i'm calculating bearing between 2 points. alpha.
with 2 values, wanted use basic trigonometry functions resolve problem.
so tried calculate:catheti_1 = sin(alpha) * hypotenuse, catheti_2 = cos(alpha) * hypotenuse
.
maybe i'm doing wrong, results useless @ moment.
so question is: how can calculate distance in x , y direction between 2 gps points?
i'm calculating alpha in following procedure:
public static double bearingto(gpsbean point1, gpsbean point2) { double lat1 = math.toradians(point1.latitude); double lat2 = math.toradians(point2.latitude); double lon1 = math.toradians(point1.longitude); double lon2 = math.toradians(point2.longitude); double deltalong = lon2 - lon1; double y = math.sin(deltalong) * math.cos(lat2); double x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(deltalong); double bearing = math.atan2(y, x); return (math.todegrees(bearing) + 360) % 360; }
i implemented code, using approximate coordinates of nyc , boston reference points, , implementing haversine formula found @ http://www.movable-type.co.uk/scripts/latlong.html (which didn't show):
long1 = -71.02; lat1 = 42.33; long2 = -73.94; lat2 = 40.66; lat1 *=pi/180; lat2 *=pi/180; long1*=pi/180; long2*=pi/180; dlong = (long2 - long1); dlat = (lat2 - lat1); // haversine formula: r = 6371; = sin(dlat/2)*sin(dlat/2) + cos(lat1)*cos(lat2)*sin(dlong/2)*sin(dlong/2) c = 2 * atan2( sqrt(a), sqrt(1-a) ); d = r * c;
when run code, d = 306
, agrees answer above site.
for bearing 52 deg - again, close site gave.
without seeing rest of code it's hard know why answer different.
note: when 2 points close together, make kinds of approximations, code should still work - formula has numerical stability because it's using sin
of difference between longitudes, latitudes (rather difference of sin).
addendum:
using code x, y (in question), sensible values distance - agreeing "proper" answer within 120 m (which isn't bad since 1 straight line approximation , other follows curvature of earth). think code ok fixed typo.
Comments
Post a Comment