Warm tip: This article is reproduced from stackoverflow.com, please click
android google-maps kotlin

How change the intent.putExtra when you click in a different marker with google map & Kotlin Android

发布于 2020-03-28 23:17:02

I want to have a different putExtra if I click with a different Marker in my google map with Kotlin Android.

    mMap.addMarker(
            MarkerOptions()
                .position(concessionnaireTest)
                .title("test")
                .snippet("Numéro : *******")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))

            )

    mMap.addMarker(
        MarkerOptions()
            .position(concessionnaireTestsecond)
            .title("test2")
            .snippet("Numéro : *******")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))

            )

    mMap.moveCamera(CameraUpdateFactory.newLatLng(concessionnaireTest))



    mMap.setOnInfoWindowClickListener {

        intent.putExtra("info", "testing")
        startActivity(intent)

    }

Thank you for helping

Questioner
Tytanpolo
Viewed
60
Bhoomi 2020-01-31 21:02

Add tag parameter while adding marker and check the marker click according to the tag as below:

 mMap.addMarker(
        MarkerOptions()
            .position(concessionnaireTest)
            .title("test")
            .snippet("Numéro : *******")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)))?.tag="test1"



mMap.addMarker(
    MarkerOptions()
        .position(concessionnaireTestsecond)
        .title("test2")
        .snippet("Numéro : *******")
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)))?.tag="test2"



mMap.moveCamera(CameraUpdateFactory.newLatLng(concessionnaireTest))

Here's how you can add marker click listener and according to tag pass the intent extras:

mMap?.setOnMarkerClickListener { marker ->
        val markerTag = marker?.tag
        markerTag?.let {
            when (markerTag) {
                "test1" -> {
                    intent.putExtra("info", "testing1")
                    startActivity(intent)

                }
                "test2" -> {
                    intent.putExtra("info", "testing2")
                    startActivity(intent)

                }
                else -> {

                }
            }
        }
        true
    }

Hope this helps!