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 library
gmaps.gs
offersgeofencing
, which is the name of this type of practice. http://hpneo.github.io/gmaps/examples/geofences.html– OnoSendai
@Onosendai, it seems to me that the library is for Javascript. I need this feature on Android.
– Geison Santos
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
– OnoSendai
That’s right, @Onosendai, native applications. I’ll take a look at the link. Thank you!
– Geison Santos
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.– Bacco
Is there any database that you store these points? If local or from a server, you can determine this directly on a query.
– Paulo Rodrigues
@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?
– Geison Santos
@Geisonsantos but are stored in which database? Sqlite in the apparatus? Mysql on the server? Sqlserver? Other?
– Paulo Rodrigues
@Paulorodrigues, the data is stored in a Mysql database.
– Geison Santos