vendredi 6 novembre 2020

Toggle map Markers with CheckBox isChecked kotlin using if/else can not be null

I'm trying to use CheckBox's to have a user select which map marker's to display. the CheckBox's are in another activity. I have tried using the CheckBoxID.isChecked, but it states can not be null. Neither of the check box's will be checked until a user clicks them. I have all of my places in a mapOf()

private val places = mapOf(
        // LOVES TRUCK STOPS
        "LOVES282" to LatLng(37.013610, -94.544720),
        "LOVES727" to LatLng(36.568478, -95.218046),
        "LOVES295" to LatLng(36.162585, -95.342562),
        "LOVES255" to LatLng(35.485822, -95.149915),
        "LOVES636" to LatLng(36.719111, -95.629642),
        // PETRO TRUCK STOPS
        "JOPLINPETRO" to LatLng(37.006505, -94.551619),
        "PETRO316" to LatLng(35.465525, -97.474443)
)

Here is the code that I had before trying to toggle the markers:

private fun addMarkersToMap() {
val placeDetailsMap = mutableMapOf(

                    "LOVES282" to PlaceDetails(
                            position = places.getValue("LOVES282"),
                            title = "LOVES282",
                            snippet = "I44 X 4"

                    ),


                    "LOVES727" to PlaceDetails(
                            position = places.getValue("LOVES727"),
                            title = "LOVES727",
                            snippet = "I44 X 283"

                    ),

                    "LOVES295" to PlaceDetails(
                            position = places.getValue("LOVES295"),
                            title = "LOVES295",
                            snippet = "US 412/US 69"
                    ),

                    "LOVES255" to PlaceDetails(
                            position = places.getValue("LOVES255"),
                            title = "LOVES255",
                            snippet = "I40 x 287"
                    ),

                    "LOVES636" to PlaceDetails(
                            position = places.getValue("LOVES636"),
                            title = "LOVES636",
                            snippet = "US 169/AIRPORT RD"
                    ),

                    "JOPLINPETRO" to PlaceDetails(
                            position = places.getValue("JOPLINPETRO"),
                            title = "JOPLINPETRO",
                            snippet = "I44 X 4"
                    ),

                    "PETRO316" to PlaceDetails(
                            position = places.getValue("PETRO316"),
                            title = "PETRO316",
                            snippet = "I35/I40 X 127"
                    ),

        )




        // place markers for each of the defined locations
        placeDetailsMap.keys.map {
            with(placeDetailsMap.getValue(it)) {
                mMap.addMarker(MarkerOptions()
                        .position(position)
                        .title(title)
                        .snippet(snippet)
                        .icon(icon)
                        .infoWindowAnchor(infoWindowAnchorX, infoWindowAnchorY)
                        .draggable(draggable)
                        .zIndex(zIndex))

            }
        }


}

This is how I tried to toggle between the markers but it states loves can not be null:

 private fun addMarkersToMap() {

    // fix the if and else to not display the certain markers.

    fun onCheckBoxClicked (view: View) {


        val placeDetailsMap = mutableMapOf(

        if (view is CheckBox) {
            val checked: Boolean = view.isChecked

            when (view.id) {
                R.id.checkBoxLoves -> {
                    if (checked) {

                                "LOVES282" to PlaceDetails(
                                        position = places.getValue("LOVES282"),
                                        title = "LOVES282",
                                        snippet = "I44 X 4"

                                )//,


                                "LOVES727" to PlaceDetails(
                                        position = places.getValue("LOVES727"),
                                        title = "LOVES727",
                                        snippet = "I44 X 283"

                                )//,

                                "LOVES295" to PlaceDetails(
                                        position = places.getValue("LOVES295"),
                                        title = "LOVES295",
                                        snippet = "US 412/US 69"
                                )//,

                                "LOVES255" to PlaceDetails(
                                        position = places.getValue("LOVES255"),
                                        title = "LOVES255",
                                        snippet = "I40 x 287"
                                )//,

                                "LOVES636" to PlaceDetails(
                                        position = places.getValue("LOVES636"),
                                        title = "LOVES636",
                                        snippet = "US 169/AIRPORT RD"
                                )//,

                                )
                        // place markers for each of the defined locations
                        placeDetailsMap.keys.map {
                            with(placeDetailsMap.getValue(it)) {
                                mMap.addMarker(MarkerOptions()
                                        .position(position)
                                        .title(title)
                                        .snippet(snippet)
                                        .icon(icon)
                                        .infoWindowAnchor(infoWindowAnchorX, infoWindowAnchorY)
                                        .draggable(draggable)
                                        .zIndex(zIndex))

                            }
                        }

                    }

                    else {
                    }
                } R.id.checkBoxPetro -> {
                if (checked) {

                    "JOPLINPETRO" to PlaceDetails(
                            position = places.getValue("JOPLINPETRO"),
                            title = "JOPLINPETRO",
                            snippet = "I44 X 4"
                    )//,

                    "PETRO316" to PlaceDetails(
                            position = places.getValue("PETRO316"),
                            title = "PETRO316",
                            snippet = "I35/I40 X 127"
                    )//,

                } else {
                }
            }
            }
        }


        // place markers for each of the defined locations
        placeDetailsMap.keys.map {
            with(placeDetailsMap.getValue(it)) {
                mMap.addMarker(MarkerOptions()
                        .position(position)
                        .title(title)
                        .snippet(snippet)
                        .icon(icon)
                        .infoWindowAnchor(infoWindowAnchorX, infoWindowAnchorY)
                        .draggable(draggable)
                        .zIndex(zIndex))

            }
        }


}

This code does give me the error of expecting an element. When I tried it with just the code like this:

private fun addMarkersToMap() {
    val loves = checkBoxLoves.isChecked
    val petroTS = checkBoxPetro.isChecked


        val placeDetailsMap = mutableMapOf(

                if (loves) {

                    "LOVES282" to PlaceDetails(
                            position = places.getValue("LOVES282"),
                            title = "LOVES282",
                            snippet = "I44 X 4"

                    )//,


                    "LOVES727" to PlaceDetails(
                            position = places.getValue("LOVES727"),
                            title = "LOVES727",
                            snippet = "I44 X 283"

                    )//,

                    "LOVES295" to PlaceDetails(
                            position = places.getValue("LOVES295"),
                            title = "LOVES295",
                            snippet = "US 412/US 69"
                    )//,

                    "LOVES255" to PlaceDetails(
                            position = places.getValue("LOVES255"),
                            title = "LOVES255",
                            snippet = "I40 x 287"
                    )//,

                    "LOVES636" to PlaceDetails(
                            position = places.getValue("LOVES636"),
                            title = "LOVES636",
                            snippet = "US 169/AIRPORT RD"
                    )//,

                } else if (petroTS) {

                    "JOPLINPETRO" to PlaceDetails(
                            position = places.getValue("JOPLINPETRO"),
                            title = "JOPLINPETRO",
                            snippet = "I44 X 4"
                    )//,

                    "PETRO316" to PlaceDetails(
                            position = places.getValue("PETRO316"),
                            title = "PETRO316",
                            snippet = "I35/I40 X 127"
                    )//,

                } else return

        )




        // place markers for each of the defined locations
        placeDetailsMap.keys.map {
            with(placeDetailsMap.getValue(it)) {
                mMap.addMarker(MarkerOptions()
                        .position(position)
                        .title(title)
                        .snippet(snippet)
                        .icon(icon)
                        .infoWindowAnchor(infoWindowAnchorX, infoWindowAnchorY)
                        .draggable(draggable)
                        .zIndex(zIndex))

            }
        }


}

This is where it tells me loves can not be null. I have tried to add ?: but that wouldn't work either. Is there another way to toggle/show/hide markers on the map? I'm very new to this so I don't know if I misunderstood the if/else statements or what. I have also tried the when. neither have worked. Thank You!




Aucun commentaire:

Enregistrer un commentaire