Detect if camera is moving, then cancel the call of a function

#1

For my maps, I’m calling an api function that’s return me json data.

This function calls an API every time the camera has stop moving (so I implement OnCameraIdleListener)

What I want is this :

  1. When the user stop moving the camera, it call the api.

BUT

  1. If the camera is moving right after, stop calling the first API call (in order to avoid the call of to much API).

Here’s my code :

    class MyMap: Fragment(), OnMapReadyCallback, GoogleMap.OnCameraIdleListener, GoogleMap.OnCameraMoveListener{
    ...
    override fun onCameraIdle() {
        val coords = mMap.cameraPosition.target
        getStops(coords)
    }
    ... 
    private fun getStops(coordonnees : LatLng) {
//        Log.i(TAG, "$coordonnees")
        var url = Constants.url
        val longitude = coordonnees.longitude
        val latitude = coordonnees.latitude
        if(longitude != 0.0 && latitude != 0.0){
            val resultActus = Fuel.get(url).responseJson { request, response, result ->
                result.fold({
                    json ->
                    parseStops(json.content) //this is my function to display marker and info window
                }, {
                    err ->
                    Log.i("error", err.toString())

                })
            }
        }

    }
}

As you can see, I’m using Fuel go get my data from the API.

What to I need to change to do what I want ? Can’t find the right topic about this…