Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 302
Jay Avatar asked Jun 09 '16 22:06

Jay


1 Answers

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

like image 153
antonio Avatar answered Sep 21 '22 07:09

antonio