Add the android-maps-utils library and use the Cluster functionality to display a nice item amount when the markers are too close.

This commit is contained in:
Severiano Jaramillo 2019-01-22 20:14:17 -06:00
parent 8c56250d1a
commit 6abcf6ed13
3 changed files with 32 additions and 21 deletions

View file

@ -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"

View file

@ -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?
)
) : ClusterItem {
override fun getSnippet(): String {
return address ?: ""
}
override fun getTitle(): String {
return name
}
override fun getPosition(): LatLng {
return LatLng(lat, lon)
}
}

View file

@ -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<Fea
private lateinit var mMap: GoogleMap
private var merchants: List<Merchant>? = null
private var mClusterManager: ClusterManager<Merchant>? = null
private lateinit var merchantIcon: BitmapDescriptor
@ -107,6 +107,13 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback<Fea
verifyLocationPermission()
// Setup clusters to group markers when possible
mClusterManager = ClusterManager(context, mMap)
// Point the map's listeners at the listeners implemented by the cluster manager.
mMap.setOnCameraIdleListener(mClusterManager)
mMap.setOnMarkerClickListener(mClusterManager)
val gson = GsonBuilder()
.setLenient()
.create()
@ -142,26 +149,14 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback<Fea
override fun onResponse(call: Call<FeathersResponse<Merchant>>, response: Response<FeathersResponse<Merchant>>) {
if (response.isSuccessful) {
val res: FeathersResponse<Merchant>? = 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()
}
}
}
override fun onFailure(call: Call<FeathersResponse<Merchant>>, t: Throwable) {
}
override fun onFailure(call: Call<FeathersResponse<Merchant>>, t: Throwable) { /* Do nothing */ }
private fun getMarkerIconFromDrawable(drawable: Drawable): BitmapDescriptor {
val canvas = Canvas()