Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a latitude and longitude is within a circle

See this illustration:

enter image description here

What I would like to know is:

  1. How to create an area (circle) when given a latitude and longitude and the distance (10 kilometers)
  2. How to check (calculate) if a latitude and longitude is either inside or outside the area

I would prefer if you can give me code example in Java or specifically for Android with Google Maps API V2

like image 745
Peter Warbo Avatar asked Feb 27 '14 09:02

Peter Warbo


Video Answer


2 Answers

What you basically need, is the distance between two points on the map:

float[] results = new float[1]; Location.distanceBetween(centerLatitude, centerLongitude, testLatitude, testLongitude, results); float distanceInMeters = results[0]; boolean isWithin10km = distanceInMeters < 10000; 

If you have already Location objects:

Location center; Location test; float distanceInMeters = center.distanceTo(test); boolean isWithin10km = distanceInMeters < 10000; 

Here is the interesting part of the API used: https://developer.android.com/reference/android/location/Location.html

like image 130
flx Avatar answered Sep 19 '22 14:09

flx


Check this:

 private boolean isMarkerOutsideCircle(LatLng centerLatLng, LatLng draggedLatLng, double radius) {     float[] distances = new float[1];     Location.distanceBetween(centerLatLng.latitude,             centerLatLng.longitude,             draggedLatLng.latitude,             draggedLatLng.longitude, distances);     return radius < distances[0]; } 
like image 45
gulab ahirwar Avatar answered Sep 16 '22 14:09

gulab ahirwar