Asad AndyDev
Asad AndyDev

Reputation: 501

Checking number of markers shown on visible region android

I am working on map app where I am getting the co-ordinates of the places from my server and I am calling the API which get me the co-ordinates from server using mMap.setOnCameraIdleListener. The problem is that the API is being called every time I move the camera and markers are being set on the same position again and again if user has just slightly move the map camera.

Now, I thought of checking the number of markers on the visible region, but I am not getting how to count the number of markers only on visible region. I used the below code but it only can tell me if one specific LatLng point is visible on the visible region or not.

public boolean isVisibleOnMap(LatLng latLng) {
    VisibleRegion vr = mMap.getProjection().getVisibleRegion();
    return vr.latLngBounds.contains(latLng);
}  

Upvotes: 3

Views: 1906

Answers (2)

Ketan Ramani
Ketan Ramani

Reputation: 5773

if(mMap.getProjection().getVisibleRegion().latLngBounds.contains(currentMarker.getPosition())){
//Showing in VisibleRegion
} else {
//Not Showing
}

Upvotes: 0

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

I am posting this as an answer:

Try using a for loop to reiterate over all markers and use vr.latLngBounds.contains(latLng); to check if these markers are contained in the visible region.

Upvotes: 5

Related Questions