How to calculate the correct distance between two Points?

Put your problem here if it does not fit any of the other categories.

How to calculate the correct distance between two Points?

Postby nk » Tue Mar 25, 2008 4:43 pm

Hi,

i just tried to calculate the Distance in meters between two static Points with the Location.distanceTo - Method.

The Problem is that the returned values are way too high - i expected the distance in meters but i get someting which is way higher.

Maybe its because of the setTime where i set the UTC-Time to the actual time (because i didnt knew what to do :) ).

Do you know whats wrong with my source?
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     public String distanceBetween(Point b){
  3.  
  4.        
  5.  
  6.         String distance = "";
  7.  
  8.        
  9.  
  10.         Location l = new Location();
  11.  
  12.         l.setLatitude(myLocation.getLatitudeE6());
  13.  
  14.         l.setLongitude(myLocation.getLongitudeE6());
  15.  
  16.        
  17.  
  18.         l.setTime(System.currentTimeMillis());
  19.  
  20.        
  21.  
  22.         Location l2 = new Location();
  23.  
  24.         l2.setLatitude(b.getLatitudeE6());
  25.  
  26.         l2.setLongitude(b.getLongitudeE6());
  27.  
  28.        
  29.  
  30.         l2.setTime(System.currentTimeMillis());
  31.  
  32.        
  33.  
  34.         distance = Float.toString( (l.distanceTo(l2) / 1000 )  ) ;
  35.  
  36.  
  37.  
  38.         return distance;
  39.  
  40.        
  41.  
  42.     }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



Im thankful for any clue :)
nk
Junior Developer
Junior Developer
 
Posts: 21
Joined: Tue Jan 29, 2008 3:23 pm

Top

Postby plusminus » Tue Mar 25, 2008 7:23 pm

Hello nk,

I assume your error is with these lines:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  l.setLatitude(myLocation.getLatitudeE6()); // and similar liens too...
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Just try Dividing by [font=Lucida Console]1E6[/font].

Yes, the usage of int and double based Lats/Lngs with Point/MapPoint/Location is pretty hard to get. :oops:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby nk » Wed Mar 26, 2008 3:45 pm

It works - great!

Thanks a lot for your help :)
nk
Junior Developer
Junior Developer
 
Posts: 21
Joined: Tue Jan 29, 2008 3:23 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 24 guests