I have an application android
that has some georeferenced points and shows certain points that the user requests with a spinner
, it is a query to the local database. The question is how to show the points that are in a radius of 1 km from my location?
criaderosMap = criaderoDao.queryBuilder()
.where(CtlPlCriaderoDao.Properties.Latitud.isNotNull()).list();
That code brings me all the points of the Database, there I could filter by a field that is related but I need to consult the closest ones within 1 km.
I would think that it can be done with a between.
One way to filter them can be from the SQL query and obtain those that are less than 1000 meters away (
distance < 1000
):Also through Android SDK you can also perform the comparison of geolocation values through the distanceTo() method
This would be an example
where the method
comparePrintInfo()
performs the comparison to print which locations are within a kilometer range.You must modify the method
getListOfPostitions()
so that it gets the list of positions to compare, probably from a service.activity_main.xml:
Android has a Location class that allows you, among other things, to calculate the distance between two points.
The method that will help you in your case is distanceTo which returns the distance in meters between two points. Here is an example of this class in action:
The closest thing I could find and it works is the following:
I have taken it from: Stackoverflow english
The distanceBetween method can help you, it returns the distance in meters between two points. You just have to iterate between the list and the starting point.
The method calculates the approximate distance in meters between two locations, and optionally the start and end links of the shortest path between them.
Reference:
https://developer.android.com/reference/android/location/Location
First fetch all markers from your database, then calculate the distance between one marker and another with the following code:
And to finish what you do is ask:
}
By the way, if you don't know how to go through your points, use a
for
and withif
which you are going to compare. So you can show all the points in 1km around.