2

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.

Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54

3 Answers3

1

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.

Stas Parshin
  • 7,973
  • 3
  • 24
  • 43
0

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

ifma
  • 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
0

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..

Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53
  • I don't need the whole region. I only need 1km radius. – Zin Win Htet Aug 24 '15 at 06:59
  • 1
    Then 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