Paul Alexander
Paul Alexander

Reputation: 2748

Android Google Maps V2 - Indicate if there are markers outside of the current VisibleRegion on screen

I've been trying to work out a method for my application to indicate when there are markers on the map that are outside of the current view/screen/bound/VisibleRegion.

For example, if there are current 5 markers on the map but the user is zoomed into a part of the map where they can't see any of the markers then I would like to be able to flag up to the user that there are markers on the map that they cannot see or something along those lines (it would be nice to be able to indicate in which direction the markers are from the current position).

I thought of using

LatLngBounds currentScreen = googleMap.getProjection()
    .getVisibleRegion().latLngBounds;


but this would only be able to tell me which markers are in the current visibleRegion.
any help would be appriciated, thanks.

Upvotes: 4

Views: 1661

Answers (1)

Stas Parshin
Stas Parshin

Reputation: 8303

First, you should save all your markers somewhere. For example, in list

List<Marker> markers = new ArrayList<>();
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(55.123, 36.456)));
markers.add(marker);

After this you can check, are there markers outside your screen

LatLngBounds currentScreen = mMap.getProjection().getVisibleRegion().latLngBounds;
for(Marker marker : markers) {
    if(currentScreen.contains(marker.getPosition())) {
        // marker inside visible region
    } else {
        // marker outside visible region
    }
}

Upvotes: 9

Related Questions