Calc distance between to Coords

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

Calc distance between to Coords

Postby sonezu » Fri Aug 06, 2010 12:39 pm

Hi!

I'm an absolute android newb, so I'm sorry for asking such a dumb question...


I've read the friendsfinder-tutorial and I have to be honest... I can't reproduce the distance calculation.

I have two coords with latitude and longitude and I want to calculate the distance between them. I've also read the solution by calculating it myself instead of using distanceTo or distanceBetween. It didn't work for me because it gave me a distance of two meters instead of about 3000m.

My Coords look like that:

double lat1 = 123.456;
double lng1 = 12.345;

double lat2 = 124.567;
double lng2 = 13.456;

I would prefer distanceTo but I can't figure out how to generate a new Location object by using my coords for that.

PLS HELP ME!

Again: I'm sorry for such a dumb question!!
sonezu
Freshman
Freshman
 
Posts: 6
Joined: Fri Aug 06, 2010 12:26 pm

Top

Re: Calc distance between to Coords

Postby jonbonazza » Fri Aug 06, 2010 1:25 pm

distanceTo() will not work for this as it takes the distance from your CURRENT location and finds the distance to a specific point.

distanceBetween() is what you want.

if you have those points defined, just do:
float[] results;
Location.distanceBetween(lat1,lng1,lat2,long2, results);
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Calc distance between to Coords

Postby sonezu » Fri Aug 06, 2010 1:41 pm

But I need a Location object, don't I?

What would those lines look like?
sonezu
Freshman
Freshman
 
Posts: 6
Joined: Fri Aug 06, 2010 12:26 pm

Re: Calc distance between to Coords

Postby jonbonazza » Fri Aug 06, 2010 1:53 pm

You shouldn't need a Location object. You could just use Location.distanceBetween(...);
That should work fine.

If you do need one, the easiest way to get a location object would be:
LocationManager lm = this.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);

All this should be unnecessary though as like I said, you should be ablt to just type Location.distanceBetween(...);
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Calc distance between to Coords

Postby sonezu » Fri Aug 06, 2010 2:09 pm

Ah ok, you're right. I don't need a location.

My last question is: how can I convert that float to a String?

String dist_string = Float.toString(dist);

Does not work ("is not applicable for the arguments (float [])")
sonezu
Freshman
Freshman
 
Posts: 6
Joined: Fri Aug 06, 2010 12:26 pm

Re: Calc distance between to Coords

Postby jonbonazza » Fri Aug 06, 2010 4:59 pm

distanceBetween() returns a float ARRAY. That means that you will have to access each element by its index. The float array returned by the distanceBetween() method contains anywhere from 1 to 3 elements.
The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2].

Basically, you only really care about results[0], so to convert that to a string, just do this:
String dist_string = Float.toString(dist[0]);
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Re: Calc distance between to Coords

Postby sonezu » Fri Aug 06, 2010 5:20 pm

Hm, I thought I had tried dist[0] but got no result. I'll try again.

It's interesting that the value is stored depending on it's length.

Thank you very much!!!!
sonezu
Freshman
Freshman
 
Posts: 6
Joined: Fri Aug 06, 2010 12:26 pm

Re: Calc distance between to Coords

Postby pranav09 » Sat Aug 07, 2010 6:28 am

Define the float array size by 1 and pass the float array object to distanceBetween(....,dist[0]).

That's it.

Thank.
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Re: Calc distance between to Coords

Postby sonezu » Sat Aug 07, 2010 11:29 am

Sadly it didn't work:

I tried

float[] dist = new float[1];

Location.distanceBetween(lat1,lng1,latX,latX, dist);
String dist_string = Float.toString(dist[0]);

I got an error running that app and printing out the value of dist_string.

When I use
Location.distanceBetween(lat1,lng1,latX,latX, dist[0]);

It shows me another error in my code in Eclipse that this not applicable.

What's wrong?
sonezu
Freshman
Freshman
 
Posts: 6
Joined: Fri Aug 06, 2010 12:26 pm

Re: Calc distance between to Coords

Postby jonbonazza » Sat Aug 07, 2010 7:21 pm

Ok, try this:

float[] dist = new float[3];

Location.distanceBetween(lat1,lng1,latX,latX, dist);
String dist_string = Float.toString(dist[0]);
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Calc distance between to Coords

Postby sonezu » Sat Aug 07, 2010 11:02 pm

The result seems to be a 7 digit number... could that be correct? Maybe divided by 1000 ? The distance is about 10 kilometres (maybe more, maybe less).
sonezu
Freshman
Freshman
 
Posts: 6
Joined: Fri Aug 06, 2010 12:26 pm

Re: Calc distance between to Coords

Postby jonbonazza » Mon Aug 09, 2010 2:19 pm

Ah, whoops. I forgot to mention that distanceBetween() returns the units in meters. Divide by 1000 and you should be good.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Calc distance between to Coords

Postby pranav09 » Tue Aug 10, 2010 5:59 am

If you want to convert the result into miles multiply like this way.

e.g.,
dist[0]*0.001*0.621

Thanks.
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 4 guests