How to mesure distance betweeen two locations

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

How to mesure distance betweeen two locations

Postby mess » Mon Mar 08, 2010 7:56 am

Hey,
I've got a problem with mesuring distance between two locations.
One of them is my location:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onLocationChanged(Location location)
  2.  
  3.               {
  4.  
  5.                  ...
  6.  
  7.                  curLocation = location;
  8.  
  9.               }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Second location i want to create myself. To test it i simply went to that place checked what curLocation.getLatitude() and curLocation.getLongitude() will give me. Then i set some coordinates for a new location:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. double lat2 =   20.9499192237854; // this is number which curLocation.getLatitude() returned when i was there
  2.  
  3. double long2 = 52.26164494037628; // this is number which curLocation.getLongitude() returned when i was there
  4.  
  5. Location loc2 = new Location("WHAT TO WRITE HERE ?!?!");
  6.  
  7. loc2.setLatitude(lat2);
  8.  
  9. loc2.setLongitude(long2);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


I try to count it in many ways like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. float[] distance = new float[2];
  2.  
  3. Location.distanceBetween(lat1, long1, lat2, long2, distance);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


or this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. curLocation.distanceTo(loc2);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


or this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. double deltaLat = lat2 - lat1;
  2.  
  3. double deltaLong = long2 - long1;
  4.  
  5. double a = Math.sin(deltaLat/2)*Math.sin(deltaLat/2) + Math.cos(lat1)*Math.cos(lat2)*Math.sin(deltaLong/2)*Math.sin(deltaLong/2);
  6.  
  7. double c = 2*Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  8.  
  9. double d = c * 6371;
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


or this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. float pk = (float) (180/3.14169);    
  2.  
  3. float a1 = lat1 / pk;  
  4.  
  5. float a2 = long1 / pk;  
  6.  
  7. float b1 = lat2 / pk;  
  8.  
  9. float b2 = long2 / pk;        
  10.  
  11. float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2);  
  12.  
  13. float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2);  
  14.  
  15. float t3 = FloatMath.sin(a1)*FloatMath.sin(b1);  
  16.  
  17. double tt = Math.acos(t1 + t2 + t3);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


All of this algorithms and methods are giving me a bad results - instead of eg.50m or 0,050 km it gives me something like 620, 4040104102 etc.

Can someone help me how to fix it?
mess
Freshman
Freshman
 
Posts: 9
Joined: Sun Mar 07, 2010 11:05 pm

Top

Postby nicholas.hauschild » Mon Mar 08, 2010 2:32 pm

Welcome mess,

Do all four of your distance methods return the same result of
something like 620, 4040104102 etc.


If so, there are two things I can think of:

1. Perhaps you are misinterpreting the value you are getting, and it actually reflects the value you expect.
-or-
2. Perhaps your assumptions are incorrect, that is, the distance between the points isn't actually 50 meters.

Could you please post the values of both Location objects' latitude and longitude? Perhaps I, or someone else here, could help you out some more if you need it.

Good luck!
-Nick
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Postby rafffel » Mon Mar 08, 2010 3:07 pm

check location.getaccuracy()
and try to invert lat on longitude maybe a mistake.
if accuracy is high it's possible it's have some trouble
rafffel
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 04, 2010 10:21 am

Postby mess » Mon Mar 08, 2010 8:21 pm

Do all four of your distance methods return the same result of
Quote:
something like 620, 4040104102 etc.


Nope it returns different numbers: eg. loc2.distanceTo(loc1) returns 42900.082

Could you please post the values of both Location objects' latitude and longitude? Perhaps I, or someone else here, could help you out some more if you need it.


double lat1 = curLocation.getLatitude(); // eg. 20.949828028678894
double long1 = curLocation.getLongitude(); // eg. 52.28138744831085
double lat2 =20.9499192237854;
double long2 =52.26164494037628;

check location.getaccuracy()
and try to invert lat on longitude maybe a mistake.
if accuracy is high it's possible it's have some trouble


loc1.getaccuracy() // eg.16 or 24 ..
loc2.getaccuracy() // eg.16 or 24 ..


I dont know, maybe problem is with creating 1 of the locations, I get the coordinates from other program and i need to make object Location with those coords- is this code ok ?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Location loc2 = new Location("WHAT TO WRITE HERE ?!?!");
  2. loc2.setLatitude(lat2);
  3. loc2.setLongitude(long2);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
mess
Freshman
Freshman
 
Posts: 9
Joined: Sun Mar 07, 2010 11:05 pm

Postby rafffel » Mon Mar 08, 2010 10:30 pm

accuracy seem look good.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. double lat1 = 20.949828028678894;
  3.  
  4. double long1 = 52.28138744831085;
  5.  
  6. double lat2 =20.9499192237854;
  7.  
  8. double long2 =52.26164494037628;
  9.  
  10.  
  11.  
  12.  
  13.  
  14. Location loc1 = new Location("location1");
  15.  
  16. loc1.setLatitude(lat1);
  17.  
  18. loc1.setLongitude(long1);
  19.  
  20. Location loc2 = new Location("location2");
  21.  
  22. loc2.setLatitude(lat2);
  23.  
  24. loc2.setLongitude(long2);
  25.  
  26.  
  27.  
  28. loc1.distanceTo(loc2);
  29.  
  30.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


try this and it's return what?
rafffel
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 04, 2010 10:21 am

Postby mess » Mon Mar 08, 2010 10:53 pm

rafffel wrote:accuracy seem look good.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. double lat1 = 20.949828028678894;
  2. double long1 = 52.28138744831085;
  3. double lat2 =20.9499192237854;
  4. double long2 =52.26164494037628;
  5.  
  6.  
  7. Location loc1 = new Location("location1");
  8. loc1.setLatitude(lat1);
  9. loc1.setLongitude(long1);
  10. Location loc2 = new Location("location2");
  11. loc2.setLatitude(lat2);
  12. loc2.setLongitude(long2);
  13.  
  14. loc1.distanceTo(loc2);
  15.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


try this and it's return what?



2053.3457



also:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     Location loc1 = new Location("Location1");
  2.         Location loc2 = new Location("Location2");
  3.         double lat1 =  52.28125870227814;
  4.         double long1 =20.949822664260864;
  5.         double lat2 =   20.9499192237854;
  6.         double long2 =52.28164494037628;
  7.        
  8.         loc1.setLatitude(lat1);
  9.         loc1.setLongitude(long1);
  10.         loc2.setLatitude(lat2);
  11.         loc2.setLongitude(long2);
  12.  
  13.  
  14. loc1.distanceTo(loc2)    //  4398272,0
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Last edited by mess on Mon Mar 08, 2010 11:46 pm, edited 1 time in total.
mess
Freshman
Freshman
 
Posts: 9
Joined: Sun Mar 07, 2010 11:05 pm

Top

Postby rafffel » Mon Mar 08, 2010 11:44 pm

It's correct values.

it's on meter distance betwen loc 1 and 2 is 2km.

i don't know why it's buggy i think control youre fix location .
rafffel
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 04, 2010 10:21 am

Postby mess » Mon Mar 08, 2010 11:55 pm

and the other one ?

loc1.distanceTo(loc2) // 4398272,0


should be like 20 m... not 4398272,0
mess
Freshman
Freshman
 
Posts: 9
Joined: Sun Mar 07, 2010 11:05 pm

Postby rafffel » Tue Mar 09, 2010 12:16 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         double lat1 =  52.28125870227814;
  2.  
  3.         double long1 =20.949822664260864;
  4.  
  5.         double lat2 =   20.9499192237854;
  6.  
  7.         double long2 =52.28164494037628;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



i think you have inverted lat1 and long1
rafffel
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 04, 2010 10:21 am

Postby hwrdprkns » Wed Mar 24, 2010 9:28 pm

Doesn't Google Maps have its own function to return the distance between two geopoints, or am I thinking of Google Earth?
The more you learn, the less you know.
hwrdprkns
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Nov 25, 2009 8:14 am

Top

Return to Map Problems

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests