Identify if coordinate set is within a radius on Android

I want to delimit a radius from a central coordinate (the red marker in the figure) and, from a set of coordinates (the green markers), check if these are within the area bounded by this radius.

  • How to identify, from a set of coordinates (latitude and longitude), if these or which of them are within a given radius used as a reference?
  • you can Configure the size of that radius?

I need a real example, an example project on Android.

insert the description of the image here

Author: Geison Santos, 2015-03-25

2 answers

As you said, if the storage is being done in a database MySQL on the server, I imagine you have a call via webservice to fetch this data. Thus, you can determine your question in the query itself.

Assuming you have a table with the columns lat and lng for the coordinates and passing their reference point (here I will use as an example the values -19.83996 e -43.94910), just do something like this:

SELECT *, (6371 *
        acos(
            cos(radians(-19.83996)) *
            cos(radians(lat)) *
            cos(radians(-43.94910) - radians(lng)) +
            sin(radians(-19.83996)) *
            sin(radians(lat))
        )) AS distance
FROM tabela HAVING distance <= 5

With this you will get all the locations that are at a distance of 5 Km from the reference point. This fits the second option that you reported, to fetch which ones are within the given radius.

Make sure this suits you the way you want it.

Haversine Formula

Going into more detail of the formula, it is well known as Haversine Formula , used in navigation to calculate the distance between two points of a sphere. In the solution is used some mathematical functions of own MySQL as calculation of sine, cosine, Radian and etc, then it can be easily transferred to another technology.

Applying this formula to the Earth, as is your case, the result is only approximate, since the Earth is not a perfect sphere because its radius varies from 6356.78 km at the poles to 6378.14 km at the equator. So, with this variation, it usually uses the value of 6371.00 Km as the radius of the Earth for this calculation.

 26
Author: Paulo Rodrigues, 2020-06-11 14:45:34

More gold!!!

SELECT *,(RAIO_TERRESTRE * 
        acos(
         cos(radians(PARAMETRO_LATITUDE)) * 
         cos(radians(COLUNA_LATITUDE)) * 
         cos(radians(PARAMETRO_LONGITUDE) - radians(COLUNA_LONGITUDE)) + 
         sin(radians(PARAMETRO_LATITUDE)) * 
         sin(radians(COLUNA_LATITUDE))
      )) AS CAMPOLATITUDE
FROM TABELA HAVING CAMPOLATITUDE <= KM
 3
Author: user113234, 2018-05-15 00:51:57