Search code examples
google-mapskotlinmylocationoverlay

How to setup onmylocationbuttonclicklistener using kotlin?


In java, the following code will be used to setup override in My Location Button.

    //add location button click listener
    map.setOnMyLocationButtonClickListener(new 
    GoogleMap.OnMyLocationButtonClickListener(){
        @Override
        public boolean onMyLocationButtonClick()
        {
            //TODO: Any custom actions
            return false;
        }
    });

However, in kotlin, I can't find any tutorial to learn how to setup the new action in kotlin. I have seen some tutorial on translating java code into kotlin. However, it is not successful.

    map.setOnMyLocationButtonClickListener( { 
        GoogleMap.OnMyLocationButtonClickListener() {
            override fun onMyLocationButtonClick() : Boolean {
                //TODO: Any custom actions
                return false;
            }
        }
    )}

It shows

Type Mismatch. Required: Boolean Found: GoogleMap.OnMyLocationButtonClickListener Expected value of type Boolean

I expected this override is worked but I can't find any tutorial about this. Can anyone solve my problem? Thank you.


Solution

  • You can just write. Kotlin supports Java SAM interfaces

    map.setOnMyLocationButtonClickListener {
        // todo
        false
    }