Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Need help with distanceBetween method and JSONObject if statement to only get results within a certain radius!

I have all the code and stuff here!

Hi I am trying to implement the distanceBetween method from http://developer.android.com/reference/android/location/Location.html#distanceBetween(double, double, double, double, float[]).

I am new to coding so I believe I am making this harder than it should be but I am not sure why. I am attempting to source that JSON Object and grab the information from FIeld1(callsign), Field2(frequency),Field21(latitude),Field24(latitude). Then I want to run the distanceBetween to find out if it is within a radius of say 350 KM. Can anyone help me with some hints or code please? Thanks!

http://stackoverflow.com/questions/32597669/how-do-i-fix-this-distancebetween-method-syntax-for-android

1 Answer

Hi Andreas,

The distanceBetween method takes two points, each expressed as a lat & long, and places the results in an array that you pass in as a parameter.

You are getting one lat/long pair from a JSON source; that's fine. You then want to "find out if it is within a radius of say 350 KM". I assume "it" is the lat/long you just got from JSON? So this points need to be within 350km of what? Do you have a lat/long pair for that location?

If you do, then you need to create an array to hold results in the form of a double. Then pass in the two lat/long pairs and this array into distanceBetween. Your answer (in metres and bearings) will be stored in the array you created.

I hope that makes sense.

Steve.