Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 970
Paul Alexander Avatar asked Jan 07 '23 21:01

Paul Alexander


1 Answers

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
    }
}
like image 155
Stas Parshin Avatar answered May 14 '23 15:05

Stas Parshin