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?) { + } }