Let's say I've a lot of makers I made all over the map. But when I click a button, I want to show only markers from a radius(1km) of my current location and dismiss everything else. Is that possible? I just don't want to pull every markers from SQLite and validate them if they are containing within 1km or not because validating millions markers will lower the app performance I thought. So, I wanted to do only in the view layer if possible.
-
I know this is an old thread but did you get any results? – Red M May 06 '18 at 23:22
-
@Red M, nope, I asked my boss to calculate it from server side(which is drupal7) since I can't solve that issue. I just needed to send a location to REST Api. – Zin Win Htet May 07 '18 at 05:31
-
Have you tried using Location.distanceBetween() or SphericalUtil.computeDistanceBetween()? – Red M May 07 '18 at 17:02
-
No, never heard of them before. – Zin Win Htet May 08 '18 at 07:44
3 Answers
I think you can't do it from the view layer.
All solutions will have full iteration over all marker's set.
Much better will be get them from sqlite by specific query.
Something like:
select * from markers
where lat between 35.45 and 35.46
and lng between 55.11 and 55.13
Of course, this will find square region, but you understand the idea.
And with proper indexes it will be much better than all markers check.
Anyway if you find any solution post it here please.

- 7,973
- 3
- 24
- 43
-
Ok, I will post it. Now I'm now doing another part of that project. – Zin Win Htet Aug 25 '15 at 03:22
Yes of course that is entirely possible. A possible solution is to go through your array of markers and calculate the distance from your current location to the marker, when your button is pressed. If it is greater than 1km, then set the marker to be false like so:
marker.setVisible(false);
See this webpage for an example of hiding a marker on click (the principle is the same, but you'll have to tweak the logic a little bit): http://googlemapapitutorial.com/hidemarker.jsp

- 3,673
- 4
- 26
- 38
-
So, I will need to check all markers right? As I said before, markers may be nearly million. Will it be ok with that amount of makers only with normal mobile processor? – Zin Win Htet Aug 24 '15 at 04:03
You can check for the visible area like this
private VisibleArea getVisibleRegion() {
if(mMap!=null){
VisibleRegion region = mMap.getProjection().getVisibleRegion();
LatLngBounds bounds = region.latLngBounds;
VisibleArea visibleArea = new VisibleArea(bounds.northeast.latitude, bounds.northeast.longitude, bounds.southwest.latitude, bounds.southwest.longitude);
return visibleArea;}
return null;
}
Now add the markers only in this area and remove rest of the markers from the array by this way it will be much faster..

- 3,949
- 4
- 23
- 53
-
-
1Then check this link http://stackoverflow.com/questions/6002563/android-how-do-i-set-the-zoom-level-of-map-view-to-1-km-radius-around-my-curren didn't tried but may help you. – Sunil Sunny Aug 24 '15 at 07:20