Search code examples
androidgoogle-mapsgpslatitude-longitude

Check if a GPS location is within a certain radius of another GPS location in android


I'm developing an Android app which takes the user's current location and shows other users' gps locations on a map which falls with a certain radius, say 20 kilometers.

For example, if I am the user, I want to see other users' locations plotted on a map who are within a radius of 20 kilometers from my location.

I am able to save my location, other users' locations and plot them on map also. I am not able to figure out if those users fall in 20 kilometers radius from my location or not.

I use Google Maps Android API v2 to plot locations on map and Parse for saving the GPS locations (latitude and longitude coordinates)

I Googled for solutions but in vain. Could anyone please provide a hint or reference or sample code on how to check if a GPS location is within a certain radius of another GPS location or not.

Any help will be appreciated.


Solution

  • It's very easy. When you add Marker into your Map just get distance from that Marker to your Current Location if distance is <= 20 then add that marker into Map. Like so

    float[] results = new float[1];
    Location.distanceBetween(oldPosition.latitude, oldPosition.longitude,
                newPosition.latitude, newPosition.longitude, results);