java - Calculate distance between two GeoLocation -


i trying calculate distance between 2 geo locations. problem have following: tried libraries :geocalc , haversine formulas, this:

public static final double rkilometers = 6371;  public static double calculationbydistance(double lat1, double lon1, double lat2, double lon2) {     double dlat = math.toradians(lat2 - lat1);     double dlon = math.toradians(lon2 - lon1);     lat1 = math.toradians(lat1);     lat2 = math.toradians(lat2);      double = math.sin(dlat / 2) * math.sin(dlat / 2) + math.sin(dlon / 2) * math.sin(dlon / 2) * math.cos(lat1) * math.cos(lat2);     double c = 2 * math.asin(math.sqrt(a));     return rkilometers * c; } 

but same wrong values options. short distances works perfectly, long distances doesn´t.

this test did:

//distance between barrow island(australia) , tavatave(madagascar) assertequals(calculationbydistance(20.82, 115.4, 18.15, 49.4), 6885, 20); //get 6875.965169284442  //here problem //distance between rio grande , glasgow assertequals(calculationbydistance(32.05, 52.11, 55.83, 4.25), 10744, 20); //get 4522.502442756569 

does know error? thank you!

i had code long ago, don't remember if wrote myself or got off someone. far remember gives pretty estimate, can try out , see if works you. (edits welcomed)

public static double distfrom(double lat1, double lng1, double lat2, double lng2) {         double earthradius = 3958.75;  //this in miles believe         double dlat = math.toradians(lat2-lat1);         double dlng = math.toradians(lng2-lng1);         double sindlat = math.sin(dlat / 2);         double sindlng = math.sin(dlng / 2);         double = math.pow(sindlat, 2) + math.pow(sindlng, 2)                 * math.cos(math.toradians(lat1)) * math.cos(math.toradians(lat2));         double c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a));         double dist = earthradius * c;          return dist; } 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -