How to make pressing on marker fill in the textViews

24 views Asked by At

I just started building my first application and I can't find any information about it. I have 2 markers and an onMarkerClick() function. I can manually create an else statement for marker2, but when there are 100 markers, it will look bad. I need to create an array LatLng, markerName, markerDescription and markerWorkTime for each marker. And a function that will draw them all.

Here's my code for this part

override fun onMapReady(googleMap: GoogleMap) {
        mGoogleMap = googleMap // Initialize GoogleMap object
        getLocationAccess() // Request location access
        googleMap.setPadding(0, 100, 0, 0) // Set padding for the map
        googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.mist_map_style_json)) // Set map style
        val kyivBounds = LatLngBounds( // Define bounds for the camera movement
            LatLng(50.371101, 30.406453),  // SW bounds
            LatLng(50.533994, 30.653280)    // NE bounds
        )
        googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(kyivBounds, 0)) // Move camera to the specified bounds
        val marker = mGoogleMap.addMarker( // Add a custom marker
            MarkerOptions()
                .position(LatLng(50.440098694585075, 30.551134092126688))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.frame))
        )
        val marker2 = mGoogleMap.addMarker(
            MarkerOptions()
                .position(LatLng(50.42696874799878, 30.564283902273196))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.frame))
        )
        marker?.tag = 0
        mGoogleMap.setOnMarkerClickListener(this)
        mGoogleMap.setOnMapClickListener (this)
    }
    override fun onMarkerClick(marker: Marker): Boolean {
        val markerCard = findViewById<CardView>(R.id.markerBubble)
        val markerName = findViewById<TextView>(R.id.markerName)
        val markerDescription = findViewById<TextView>(R.id.markerDescription)
        val markerWorkTime = findViewById<TextView>(R.id.workTimeActual)
        val clickCount = marker.tag as? Int // Retrieve the data from the marker.
        clickCount?.let { // Check if a click count was set, then change visibility of object
            val newClickCount = it + 1
            marker.tag = newClickCount
            markerCard.visibility = VISIBLE
            markerName.text = "Парк вічної слави"
            markerDescription.text = "Міський парк на горі з меморіалом та деревами"
            markerWorkTime.text = "Щодня"
        }
        return false
    }
0

There are 0 answers