Reputation: 723
I am showing a list of n markers in a map, then I make zoom to my location and I would like to know if its possible to count just how many markers are displayed in my area.
Thanks.
Upvotes: 0
Views: 779
Reputation: 4371
You can call below method for each marker to see if they are inside the Maps visible area.
map.getBounds().contains(marker.getPosition())
If the getBounds method is not available try this:
VisibleRegion region = map.getProjection().getVisibleRegion();
LatLngBounds mapBound = region.latLngBounds;
int count 0;
for(Marker marker : makers) { // markers is the List of marker you have
if(mapBound.contains(marker.getPosition()){
count = count + 1;
}
}
Upvotes: 1
Reputation: 6073
You can keep a List of marker within your Activity
or Fragment
and then do a loop on it to see if its in the visible area List.size()
to get the number of marker.
private List<Marker> mMarkerArray = new ArrayList<Marker>();
///to get number of marker
int marker_count = = 0;
for(int i =0;i<=mMarkerArray.size();i++){
if(mMap.latLngBounds.contains(new LatLng(mMarkerArray.get(i).getLocation().getLongitude(), mMarkerArray.get(i).getLocation().getLatitude())){
marker_count++;
}
}
just make sure you add the marker in the list after you add it to your map object
Upvotes: 1