From 6aad0ca624fb5b7ba95be236bf91f16d4c1705d9 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Wed, 23 Jan 2019 12:42:19 -0600 Subject: [PATCH] Override the method that is called when a cluster is tapped, so that the map updated the camera focus on an area that shows all the items from the selected cluster. --- .../fragments/MerchantsFragment.kt | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt index 6354697..0ae2bbe 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt @@ -23,15 +23,21 @@ import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import android.preference.PreferenceManager import androidx.core.content.ContextCompat +import com.google.android.gms.maps.CameraUpdateFactory import com.google.android.gms.maps.model.* +import com.google.maps.android.clustering.Cluster import com.google.maps.android.clustering.ClusterManager import cy.agorise.bitsybitshareswallet.database.entities.Merchant import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.MerchantMarkerRenderer import cy.agorise.bitsybitshareswallet.utils.toast +import java.lang.Exception -class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback> { +class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback>, + ClusterManager.OnClusterClickListener, + ClusterManager.OnClusterItemClickListener, + ClusterManager.OnClusterItemInfoWindowClickListener{ companion object { private const val TAG = "MerchantsFragment" @@ -110,6 +116,13 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback>, t: Throwable) { /* Do nothing */ } + + /** + * Animates the camera update to focus on an area that shows all the items from the cluster that was tapped. + */ + override fun onClusterClick(cluster: Cluster?): Boolean { + val builder = LatLngBounds.builder() + val merchantMarkers = cluster?.items + + if (merchantMarkers != null) { + for (item in merchantMarkers) { + val merchantPosition = item.position + builder.include(merchantPosition) + } + + val bounds = builder.build() + + try { + mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100)) + } catch (e: Exception) { + Log.d(TAG, e.message) + } + } + + return true + } + + override fun onClusterItemClick(p0: Merchant?): Boolean { + return false + } + + override fun onClusterItemInfoWindowClick(p0: Merchant?) { + } }