Alfred
Alfred

Reputation: 3

Reveal markers around a selected location on Google Maps

I am currently coding on Android Studio for Google Maps.

I made the search function working based on the text searched on the search bar. And once the user presses "Search" the map will zoom into that selected location and place a coloured marker.

However, I would like to do it like this:

Once you select the location, you will reveal several markers based on a certain radius/ certain area.

Upvotes: 0

Views: 805

Answers (1)

antonio
antonio

Reputation: 18262

You can add the markers to your map as invisible and compute the distance between your desired location and each marker to show those nearer than a given distance.

private List<Marker> markers = new ArrayList<>(); // List to hold your markers

Add the markers to the map and to the list:

Marker marker = mMap.addMarker(new MarkerOptions().position(yourPosition).visible(false));
markers.add(marker);

And then create a function that, given a LatLng computes the distance to each marker (I'm using SphericalUtil.computeDistanceBetween from the Google Maps Android API Utility Library) and shows the desired markers:

private void showMarkers(LatLng location, float distance) {
    for(Marker marker : markers) {
        if (SphericalUtil.computeDistanceBetween(marker.getPosition(), location) <= distance) {
            marker.setVisible(true);
        } else {
            marker.setVisible(false);
        }
    }
}

Upvotes: 1

Related Questions