diff --git a/app/build.gradle b/app/build.gradle index 46b3b43..4b1a404 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -66,6 +66,7 @@ dependencies { implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.google.android.material:material:1.0.0' implementation 'com.google.android.gms:play-services-maps:16.0.0' + implementation 'com.google.maps.android:android-maps-utils:0.5+' // AAC Lifecycle implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/entities/Merchant.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/entities/Merchant.kt index 7048a1e..fadf7b1 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/entities/Merchant.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/entities/Merchant.kt @@ -3,6 +3,8 @@ package cy.agorise.bitsybitshareswallet.database.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey +import com.google.android.gms.maps.model.LatLng +import com.google.maps.android.clustering.ClusterItem @Entity(tableName = "merchants") data class Merchant( @@ -10,9 +12,22 @@ data class Merchant( @ColumnInfo(name = "id") val _id: String, @ColumnInfo(name = "name") val name: String, @ColumnInfo(name = "address") val address: String?, - @ColumnInfo(name = "lat") val lat: Float, - @ColumnInfo(name = "lon") val lon: Float, + @ColumnInfo(name = "lat") val lat: Double, + @ColumnInfo(name = "lon") val lon: Double, @ColumnInfo(name = "phone") val phone: String?, @ColumnInfo(name = "telegram") val telegram: String?, @ColumnInfo(name = "website") val website: String? -) \ No newline at end of file +) : ClusterItem { + override fun getSnippet(): String { + return address ?: "" + } + + override fun getTitle(): String { + return name + } + + override fun getPosition(): LatLng { + return LatLng(lat, lon) + } + +} \ No newline at end of file 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 5413b95..60b150e 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt @@ -21,13 +21,13 @@ import retrofit2.Call import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory -import java.io.IOException import android.graphics.Bitmap import android.graphics.Canvas import android.graphics.drawable.Drawable import android.preference.PreferenceManager import androidx.core.content.ContextCompat import com.google.android.gms.maps.model.* +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.toast @@ -44,7 +44,7 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback? = null + private var mClusterManager: ClusterManager? = null private lateinit var merchantIcon: BitmapDescriptor @@ -107,6 +107,13 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback>, response: Response>) { if (response.isSuccessful) { val res: FeathersResponse? = response.body() - merchants = res!!.data - for (mer in merchants!!) { - val location = LatLng(mer.lat.toDouble(), mer.lon.toDouble()) - mMap.addMarker( - MarkerOptions().position(location).title(mer.name).snippet(mer.address).icon(merchantIcon) - ) - } + val merchants = res?.data ?: return + mClusterManager?.addItems(merchants) } else { - try { - Log.e("error_bitsy", response.errorBody()?.string()) - } catch (e: IOException) { - e.printStackTrace() - } - + Log.e("error_bitsy", response.errorBody()?.string()) } } - override fun onFailure(call: Call>, t: Throwable) { - - } + override fun onFailure(call: Call>, t: Throwable) { /* Do nothing */ } private fun getMarkerIconFromDrawable(drawable: Drawable): BitmapDescriptor { val canvas = Canvas()