1

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.

2 Answers2

1

You can just write. Kotlin supports Java SAM interfaces

map.setOnMyLocationButtonClickListener {
    // todo
    false
}
Andrei Tanana
  • 7,932
  • 1
  • 27
  • 36
  • It works. Thank you. If I want to use the value of latitude and longitude after I press the my location button. How can I achieve this? – Chu Man Ki Morris Sep 19 '19 at 13:19
  • the short answer is: you can use `map.myLocation`. The long answer is here https://developers.google.com/maps/documentation/android-sdk/current-place-tutorial#get-the-location-of-the-android-device-and-position-the-map – Andrei Tanana Sep 19 '19 at 14:02
0

try

map.setOnMyLocationButtonClickListener(object :
            GoogleMap.OnMyLocationButtonClickListener {
            override fun onMyLocationButtonClick(): Boolean {

                return false
            }
        })
Madhav
  • 317
  • 2
  • 12