Check if a GPS location is within a certain radius of another GPS location in android
Solution 1
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);
Solution 2
First get distance between 2 position:
float[] distance = new float[1];
Location.distanceBetween(oldPosition.latitude, oldPosition.longitude,
currentPosition.latitude, currentPosition.longitude, distance);
distance will be in meter.
So, we have to convert our designer radius (20 KM) distance in meter to compare.
double radiusInMeters = 20.0*1000.0; //1 KM = 1000 Meter
New we can check that current position is inside 20 KM radius or not using...
if( distance[0] > radiusInMeters ){
Toast.makeText(getBaseContext(),
"Outside, distance from center: " + distance[0] + " radius: " + radiusInMeters,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(),
"Inside, distance from center: " + distance[0] + " radius: " + radiusInMeters ,
Toast.LENGTH_LONG).show();
}
Related videos on Youtube
Comments
-
athira09 over 1 year
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.
-
athira09 over 9 yearsWhat is results used for? Can you just explain.
-
M D over 9 years@athi09 results variable contains distance.
-
athira09 over 9 yearswhy is it float[1] ?
-
M D over 9 years@athi09 Just and array with two elements
-
M D over 9 years
-
athira09 over 9 yearsThank you M D. I'll just try this out and let you know the result
-
athira09 over 9 yearsI'm getting some value. Is the results shown in meters?
-
AlexWien over 9 years@athi09 you should be able to find out yourself what value distancebetween delivers. Since it is an API function, just read the online docu.
-
athira09 over 9 yearsdistanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.
-
athira09 over 4 yearsThank you @Enamul but I already got the answer and I have accepted it too.