Search code examples
react-nativemaps

onRegionChangeComplete is called too many times, and not when really the event ends


This method is called when i not ended to pinch the map, is there anyway to fix this?

https://i.sstatic.net/GHQNc.jpg

Here is the code

<MapView
    style={{flex:1}}
    region={{
    latitude: parseFloat(this.state.initialPosition.coords.latitude),
        longitude: parseFloat(this.state.initialPosition.coords.longitude),
    latitudeDelta,
    longitudeDelta,
    }}
    showsUserLocation={true}
    onRegionChangeComplete={(region) => {
         console.log('onRegionChangeComplete called!');
    }}
/>

Solution

  • i justo solve it making a setTimeout! here is how

    <MapView
        style={{flex:1}}
        showsUserLocation={true}
        onRegionChange={(region) => {
            if(this.timeout){
                clearTimeout(this.timeout);
            }
            this.timeout = setTimeout(() => {
                    this.getDoctorsByGeolocation(region);
            },1000);
        }}
    />
    

    i hope this can help anyone!