I have found that there's mostly just Java code examples on Google's development site, and very few in Kotlin too which is pretty annoying so I have to ask this here.
I'm trying to setup a ClusterManager, but i don't have any clues how it's supposed to be done, and there's no Kotlin examples to be seen. I did manage this much:
override fun onMapReady(googleMap: GoogleMap) {
// return early if the map was not initialised properly
mMap = googleMap ?: return
with(mMap.uiSettings) {
isZoomControlsEnabled = true
isMyLocationButtonEnabled = true
isCompassEnabled = true
isRotateGesturesEnabled = true
isZoomGesturesEnabled = true
}
val clusterManager = ClusterManager<ScootMarker>(this, mMap)
mMap.setOnCameraIdleListener(clusterManager)
mMap.setOnMarkerClickListener(clusterManager)
mMap.setOnInfoWindowClickListener(clusterManager)
mMap.setInfoWindowAdapter(CustomInfoWindowAdapter(this))
mMap.mapType = GoogleMap.MAP_TYPE_HYBRID
setUpMap()
getData()
}
I however don't know how I'm supposed to add markers to that ClusterManager, nor how to bind it to my CustomInfoWindowAdapter.
I already have my own custom item done, which returns the snippet, location and title.
Please try the code below for guidance and demonstration of how to add a marker clusterer in Kotlin.
class MyItem : ClusterItem {
private val mPosition: LatLng
private val mTitle: String
private val mSnippet: String
constructor(lat: Double, lng: Double) {
mPosition = LatLng(lat, lng)
mTitle = ""
mSnippet = ""
}
constructor(lat: Double, lng: Double, title: String, snippet: String) {
mPosition = LatLng(lat, lng)
mTitle = title
mSnippet = snippet
}
override fun getPosition(): LatLng {
return mPosition
}
override fun getTitle(): String {
return mTitle
}
override fun getSnippet(): String {
return mSnippet
}
}
class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener {
private lateinit var mMap: GoogleMap
private lateinit var mClusterManager: ClusterManager<MyItem>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
setUpClusterer()
mMap.setOnInfoWindowClickListener(this);
}
private fun setUpClusterer() {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(51.503186, -0.126446), 10f))
mClusterManager = ClusterManager(this, mMap)
mMap.setOnCameraIdleListener(mClusterManager)
mMap.setOnMarkerClickListener(mClusterManager)
addItems()
}
private fun addItems() {
var lat = 51.5145160
var lng = -0.1270060
for (i in 0..9) {
val offset = i / 60.0
lat = lat + offset
lng = lng + offset
val title = "This is the title"
val snippet = "and this is the snippet."
val offsetItem = MyItem(lat, lng, title, snippet)
mClusterManager.addItem(offsetItem)
}
}
override fun onInfoWindowClick(marker: Marker) {
Toast.makeText(
this, "Info window clicked",
Toast.LENGTH_SHORT
).show()
}
}
Note that this is based on the Java code implementation from Google's guides [1] [2] and it works without problem for me, so I hope this helps you!
[1] https://developers.google.com/maps/documentation/android-sdk/utility/marker-clustering
[2] https://developers.google.com/maps/documentation/android-sdk/infowindows