Jay
Jay

Reputation: 145

Android - display in the map only the markers included in a determinate area

I have my application with a map. In this map i put a marker in the current location of the device. I also add a circle around the marker as follow:

    Circle circle = mMap.addCircle(new CircleOptions()
                        .center(latLng)
                        .radius(400)     //The radius of the circle, specified in meters. It should be zero or greater.
                        .strokeColor(Color.rgb(0, 136, 255))
                        .fillColor(Color.argb(20, 0, 136, 255)));

The result is something like this:
here's an example of the result!

I have a database with some positions characterized by a latitude and a longitude.

I would set markers in the map, only for positions that are located within the circle added previously.
How can I understand which of them are included in that area?

please help me, thanks!

Upvotes: 5

Views: 4323

Answers (2)

antonio
antonio

Reputation: 18242

You can add all your markers, making them invisible at first, and then compute the distance between the center of your circle and your markers, making visible the markers that are within a given distance:

private List<Marker> markers = new ArrayList<>();

// ...

private void drawMap(LatLng latLng, List<LatLng> positions) {
    for (LatLng position : positions) {
        Marker marker = mMap.addMarker(
                new MarkerOptions()
                        .position(position)
                        .visible(false)); // Invisible for now
        markers.add(marker);
    }

    //Draw your circle
    Circle circle = mMap.addCircle(new CircleOptions()
            .center(latLng)
            .radius(400)
            .strokeColor(Color.rgb(0, 136, 255))
            .fillColor(Color.argb(20, 0, 136, 255)));

    for (Marker marker : markers) {
        if (SphericalUtil.computeDistanceBetween(latLng, marker.getPosition()) < 400) {
            marker.setVisible(true);
        }
    }
}

Note that I'm using the SphericalUtil.computeDistanceBetween method from the Google Maps API Utility Library

Upvotes: 5

Yasin Ka&#231;maz
Yasin Ka&#231;maz

Reputation: 6663

You can take a look at this question for how to calculate distance between two latitude longitude : how-to-calculate-distance-between-two-locations-using-their-longitude-and-latitu

İf distance between 2 point is smaller than your Circle's radius(for you 400) put marker for them .(Also dont look only selected answer.There is selected_location.distanceTo(another_location) will help you in below answer. )

Upvotes: 0

Related Questions