Add custom marker info window to teller markers, to show the complete information as with the merchants.

This commit is contained in:
Severiano Jaramillo 2019-01-24 19:18:59 -06:00
parent 2e07bc512f
commit 285a4b6c49

View file

@ -50,10 +50,13 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
private var mMarkerManager: MarkerManager? = null private var mMarkerManager: MarkerManager? = null
// Cluster managers to create custom merchants and tellers clusters with a custom behavior too
private var mMerchantClusterManager: ClusterManager<Merchant>? = null private var mMerchantClusterManager: ClusterManager<Merchant>? = null
private var mTellerClusterManager: ClusterManager<Teller>? = null private var mTellerClusterManager: ClusterManager<Teller>? = null
// Variables to keep track of the currently selected merchant and teller
private var selectedMerchant: Merchant? = null private var selectedMerchant: Merchant? = null
private var selectedTeller: Teller? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_merchants, container, false) return inflater.inflate(R.layout.fragment_merchants, container, false)
@ -100,13 +103,14 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
verifyLocationPermission() verifyLocationPermission()
// User MarkerManager to be able to use Map events in more than one ClusterManager
mMarkerManager = MarkerManager(mMap) mMarkerManager = MarkerManager(mMap)
initMerchantsCluster() initMerchantsCluster()
initTellersCluster() initTellersCluster()
// Point the map's listeners at the listeners implemented by the cluster manager. // Point the map's listeners at the listeners implemented by the marker manager.
mMap.setOnMarkerClickListener(mMarkerManager) mMap.setOnMarkerClickListener(mMarkerManager)
mMap.setOnCameraIdleListener { mMap.setOnCameraIdleListener {
@ -148,7 +152,7 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
} }
private fun initMerchantsCluster() { private fun initMerchantsCluster() {
// Setup clusters to group markers when possible // Setup clusters to group markers when possible using a custom renderer
mMerchantClusterManager = ClusterManager(context, mMap, mMarkerManager) mMerchantClusterManager = ClusterManager(context, mMap, mMarkerManager)
val merchantRenderer = MerchantClusterRenderer(context, mMap, mMerchantClusterManager) val merchantRenderer = MerchantClusterRenderer(context, mMap, mMerchantClusterManager)
mMerchantClusterManager?.renderer = merchantRenderer mMerchantClusterManager?.renderer = merchantRenderer
@ -159,6 +163,7 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
false false
} }
// Force marker to use a custom info window
mMerchantClusterManager?.markerCollection?.setOnInfoWindowAdapter(MerchantInfoWindowAdapter()) mMerchantClusterManager?.markerCollection?.setOnInfoWindowAdapter(MerchantInfoWindowAdapter())
mMerchantViewModel.getAllMerchants().observe(this, Observer<List<Merchant>> {merchants -> mMerchantViewModel.getAllMerchants().observe(this, Observer<List<Merchant>> {merchants ->
@ -169,13 +174,19 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
} }
private fun initTellersCluster() { private fun initTellersCluster() {
// Setup clusters to group markers when possible // Setup clusters to group markers when possible using a custom renderer
mTellerClusterManager = ClusterManager(context, mMap, mMarkerManager) mTellerClusterManager = ClusterManager(context, mMap, mMarkerManager)
val tellerRenderer = TellerClusterRenderer(context, mMap, mTellerClusterManager) val tellerRenderer = TellerClusterRenderer(context, mMap, mTellerClusterManager)
mTellerClusterManager?.renderer = tellerRenderer mTellerClusterManager?.renderer = tellerRenderer
mTellerClusterManager?.setOnClusterClickListener { onClusterClick(it as Cluster<ClusterItem>) } mTellerClusterManager?.setOnClusterClickListener { onClusterClick(it as Cluster<ClusterItem>) }
mTellerClusterManager?.setOnClusterItemClickListener { false } mTellerClusterManager?.setOnClusterItemClickListener { teller ->
selectedTeller = teller
false
}
// Force marker to use a custom info window
mTellerClusterManager?.markerCollection?.setOnInfoWindowAdapter(TellerInfoWindowAdapter())
mMerchantViewModel.getAllTellers().observe(this, Observer<List<Teller>> {tellers -> mMerchantViewModel.getAllTellers().observe(this, Observer<List<Teller>> {tellers ->
mTellerClusterManager?.clearItems() mTellerClusterManager?.clearItems()
@ -206,6 +217,7 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
return true return true
} }
/** Creates a custom view for the Merchant's Info Window, when a merchant marker is selected */
inner class MerchantInfoWindowAdapter : GoogleMap.InfoWindowAdapter { inner class MerchantInfoWindowAdapter : GoogleMap.InfoWindowAdapter {
override fun getInfoWindow(marker: Marker?): View { override fun getInfoWindow(marker: Marker?): View {
@ -220,7 +232,7 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
if (selectedMerchant != null) { if (selectedMerchant != null) {
tvName.text = selectedMerchant?.name tvName.text = selectedMerchant?.name
if (selectedMerchant?.name != null) if (selectedMerchant?.address != null)
tvAddress.text = selectedMerchant?.address tvAddress.text = selectedMerchant?.address
else else
tvAddress.visibility = View.GONE tvAddress.visibility = View.GONE
@ -241,9 +253,7 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
?.removePrefix("http://")?.removePrefix("https://") ?.removePrefix("http://")?.removePrefix("https://")
else else
tvWebsite.visibility = View.GONE tvWebsite.visibility = View.GONE
} }
return infoWindowLayout return infoWindowLayout
} }
@ -252,5 +262,48 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback {
} }
} }
/** Creates a custom view for the Teller's Info Window, when a teller marker is selected */
inner class TellerInfoWindowAdapter : GoogleMap.InfoWindowAdapter {
override fun getInfoWindow(marker: Marker?): View {
val infoWindowLayout: View = LayoutInflater.from(context).inflate(
R.layout.marker_info_window, null)
val tvName = infoWindowLayout.findViewById<TextView>(R.id.tvName)
val tvAddress = infoWindowLayout.findViewById<TextView>(R.id.tvAddress)
val tvPhone = infoWindowLayout.findViewById<TextView>(R.id.tvPhone)
val tvTelegram = infoWindowLayout.findViewById<TextView>(R.id.tvTelegram)
val tvWebsite = infoWindowLayout.findViewById<TextView>(R.id.tvWebsite)
if (selectedTeller != null) {
tvName.text = selectedTeller?.gt_name
if (selectedTeller?.address != null)
tvAddress.text = selectedTeller?.address
else
tvAddress.visibility = View.GONE
if (selectedTeller?.phone != null)
tvPhone.text = selectedTeller?.phone
else
tvPhone.visibility = View.GONE
if (selectedTeller?.telegram != null) {
val telegram = "Telegram: ${selectedTeller?.telegram}"
tvTelegram.text = telegram
} else
tvTelegram.visibility = View.GONE
if (selectedTeller?.url != null)
tvWebsite.text = selectedTeller?.url
?.removePrefix("http://")?.removePrefix("https://")
else
tvWebsite.visibility = View.GONE
}
return infoWindowLayout
}
override fun getInfoContents(marker: Marker?): View? {
return null
}
}
} }