## 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?

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?
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.012 seconds, using GeSHi 1.0.8.4

Im thankful for any clue
nk
Junior Developer

Posts: 21
Joined: Tue Jan 29, 2008 3:23 pm

Hello nk,

I assume your error is with these lines:
Using java Syntax Highlighting
1.  l.setLatitude(myLocation.getLatitudeE6()); // and similar liens too...
Parsed in 0.010 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.

Regards,
plusminus

| Android Development Community / Tutorials

plusminus

Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

It works - great!

Thanks a lot for your help
nk
Junior Developer

Posts: 21
Joined: Tue Jan 29, 2008 3:23 pm