Skip to content Skip to sidebar Skip to footer

How Can I Get The Visible Markers In Google Maps V2 In Android?

In Google Maps v2 for Android, how can I get the visible markers? I know I can use Projection and eliminate points < 0 and points > screen size. But I do not wish to check on

Solution 1:

Ok, the following is the code the I have used before to determine what the user can see and then only draw the markers that are visible. I think you might be able to adapt it to your purpose.

get the current rectangle "viewport" of the map (note:must be run on the main thread)

this.mLatLngBounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;

sort the 2 points (top-left and bottom-right) so that we can use min/max logic

double lowLat;
double lowLng;
double highLat;
double highLng;

if (this.mLatLngBounds.northeast.latitude < this.mLatLngBounds.southwest.latitude)
{
lowLat = this.mLatLngBounds.northeast.latitude;
highLat = this.mLatLngBounds.southwest.latitude;
}
else
{
highLat = this.mLatLngBounds.northeast.latitude;
lowLat = this.mLatLngBounds.southwest.latitude;
}
if (this.mLatLngBounds.northeast.longitude < this.mLatLngBounds.southwest.longitude)
{
lowLng = this.mLatLngBounds.northeast.longitude;
highLng = this.mLatLngBounds.southwest.longitude;
}
else
{
highLng = this.mLatLngBounds.northeast.longitude;
lowLng = this.mLatLngBounds.southwest.longitude;
}

then in my case I had this data in a db, so i could use >= and <= to extract only the pins i wanted

Solution 2:

You could use the android-map-extension library. Amongst others it offers a List GoogleMap.getDisplayedMarkers() method.

Solution 3:

You could add this Extension Function to GoogleMap class if you are using Kotlin

fun GoogleMap.isMarkerVisible(markerPosition: LatLng) =
        projection.visibleRegion.latLngBounds.contains(markerPosition)

You just pass the marker position as a parameter of this method and do whatever you want with the result.

If you are using Java you can just declare the function wherever fits better for you.

Hope it helps!

Solution 4:

You are looking for either markers: https://developers.google.com/maps/documentation/android/marker

private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(newMarkerOptions()
    .position(newLatLng(0, 0))
    .title("Hello world"));

Or direct drawing: https://developers.google.com/maps/documentation/android/shapes

// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
    .add(new LatLng(37.35, -122.0))
    .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
    .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
    .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
    .add(new LatLng(37.35, -122.0)); // Closes the polyline.// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions); 

Post a Comment for "How Can I Get The Visible Markers In Google Maps V2 In Android?"