1

Google Maps has a function that allows you to pass any number of arguments to it. But I don't know how many arguments I want to pass in up front :(

Currently my code looks like this. It has arguments added in, but this is hardcoded. I want this to be variable. How can I do this in Kotlin?

polyLine= gMap.addPolyline(
            PolylineOptions()
                .add(_moments.get(0).position,
                    _moments.get(1).position,
                    _moments.get(2).position
                )
          )

Something like this?

polyLine= gMap.addPolyline(
            PolylineOptions()
                .add( for (i in 1..MAX_LIST_COUNT) {_moments.get(i).position} )
          )

Thanks for any help guys!

1

1 Answer 1

3

You can use addAll instead:

PolylineOpions().addAll(_moments.map { it.position })

If you are just interested on how to pass something to a vararg method, then have a look at the spread operator for vararg functions. An example usage:

PolylineOptions().add(*_moments.map { it.position }.toTypedArray())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.