Identify if set of coordinates is within a radius on Android

Asked

Viewed 13,123 times

14

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 that 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 certain radius used as reference?
  • It is possible set size of that radius?

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

inserir a descrição da imagem aqui

  • 1

    The library gmaps.gs offers geofencing, which is the name of this type of practice. http://hpneo.github.io/gmaps/examples/geofences.html

  • @Onosendai, it seems to me that the library is for Javascript. I need this feature on Android.

  • 1

    The lib is operational via Chrome for Android. Maybe you mean a resource for native applications (I just noticed the android API tag) - in this case: http://developer.android.com/training/location/geofencing.html

  • That’s right, @Onosendai, native applications. I’ll take a look at the link. Thank you!

  • 3

    There must be something ready for that already, but in the worst case you can just use a distance calculation: distancia = sqrt( dx * dx + dy * dy ) where dx and dy are the differences between the center and the position of the person. Note that this works for small distances (a few KM, and at a certain distance from the poles), as it does not take into account the sphericity of the earth.

  • Is there any database that you store these points? If local or from a server, you can determine this directly on a query.

  • @Paulorodrigues, I have this set of coordinates stored, yes. I don’t need to make use of Geofance, as you quoted @Onesendai? How would that approach?

  • @Geisonsantos but are stored in which database? Sqlite in the apparatus? Mysql on the server? Sqlserver? Other?

  • @Paulorodrigues, the data is stored in a Mysql database.

Show 4 more comments

2 answers

28


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. Therefore, you can determine your question on the query.

Assuming you have a table with columns lat and lng for the coordinates and passing their reference point (here I will use as an example the values -19.83996 and -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 sites that are at a distance of 5 km of the reference point. This fits into the second option you reported, to search for which are within the given radius.

Make sure it suits you the way you want it.

Haversine’s formula

Going into more detail of the formula, it is well known as Haversine’s formula, used in navigation to calculate the distance between two points of a sphere. In the solution some mathematical functions of the sphere itself are used Mysql as a calculation of sine, cosine, radian etc, then it can be easily transferred to another technology.

Applying this formula to the Earth, as is the 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, you usually use the value of 6371,00 km as the Earth’s radius for this calculation.

  • The consultation worked perfectly. I made a simulated query, Paulo, where I included 3 points close to my central point (the user’s location point) and two points more distant, at a distance of about 4km. In the DISTANCE filter in the query I reported 3 (3km) and as expected it returned only the points that are within this perimeter.

  • One last curiosity where did you find this formula? And what is the meaning of the number 6371 in the query?

  • 1

    This formula I use a lot in my applications, it’s been a while since I had this need and found it on the internet. This value represents the approximate value of the earth beam, in this case is 6.371 km.

  • Paul just to close the topic with a golden key, only highlights in his answer the formula used to calculate the distance. Indicating what each thing is, for example, which is 6371. It would look something like this: Distance calculation formula: (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))).

  • I detailed a little more based on Wikipedia.

  • Excellent response, +1.

Show 1 more comment

3

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
  • That answer has already been find here, and besides I didn’t understand any of your reply. I could edit your reply and detail better???

Browser other questions tagged

You are not signed in. Login or sign up in order to post.