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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With