I am trying to filter out my clusters/markers based on a user's location with a predefined Circle for the radius. I would like it so that clusters/markers are invisible outside that radius.
I have tried creating an array and putting my lat and longs such as this: Android - display in the map only the markers included in a determinate area
and this: How to show markers only inside of radius (circle) on maps?
However, I am not sure how to approach this problem using ClusterManager
Here I get my data from Firebase and store it in ClusterManager:
private fun loadMarkersFromDB() {
mCompanies.child("data/results").addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists()) {
for (s in dataSnapshot.children) {
var company = s.getValue(Company::class.java)
val infoWindow =
MyItem(company!!.latitude, company!!.longitude, company.name)
mClusterManager.setAnimation(false)
mClusterManager.addItem(infoWindow)
Log.d("onDataChange", company.toString())
}
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.w("MapsActivity", databaseError.toException())
}
})
}
Setting up Cluster function:
private fun setUpCluster() {
// Initialize the manager with the context and the map.
// (Activity extends context, so we can pass 'this' in the constructor.)
mClusterManager = ClusterManager(this, mMap)
// Point the map's listeners at the listeners implemented by the cluster
// manager.
mMap.setOnCameraIdleListener(mClusterManager)
mMap.setOnMarkerClickListener(mClusterManager)
// Add cluster items (markers) to the cluster manager.
loadMarkersFromDB()
}
What I call inside onMapReady:
setUpCluster()
mClusterManager = ClusterManager(this, mMap)
val customRenderer = CustomClusterRenderer(this, mMap, mClusterManager, mMap.getCameraPosition().zoom, 20f)
mClusterManager.renderer = customRenderer
mMap.setOnCameraMoveListener(customRenderer)
mMap.setOnCameraIdleListener(mClusterManager)
mMap.setOnMarkerClickListener(mClusterManager)
Here is a Circle code that I got from StackOverflow:
circle = mMap.addCircle(
CircleOptions()
.center(currentLatLng)
.radius(400.0) //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))
)
My Cluster Item Class:
class MyItem : ClusterItem {
private val position: LatLng
private var title: String = ""
private var snippet: String = ""
constructor(lat: Double, lng: Double, title: String) {
position = LatLng(lat, lng)
this.title = title
}
override fun getSnippet(): String {
return snippet
}
override fun getTitle(): String {
return title
}
override fun getPosition(): LatLng {
return position
}
}
I got the Clusters working, and the data has loaded as intended, however, because I have over 3000 locations, I am trying to make it as optimized as possible, so that the user can only see the markers within the given Radius, but I would also like the option to unhide the markers outside the Radius with a click of a button.