Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use animateToRegion function in react native maps?

Tags:

react-native

I am using MapView of react-native map clustering and Marker and callout of react-native-maps. I am unable to use animateToRegion. It shows me this.mapView.animateToRegion is not a function

 <MapView
 ref={map=>{mapView = map}}
 provider='google'
 clustering={true}
 onClusterPress={this.onPressCluster}
 region={this.state.region}
 onRegionChange={this.onRegionChange}
 onRegionChangeComplete={this.onRegionChangeComplete}
 style={styles.map}
 showsUserLocation={true}
 followUserLocation={true}
 zoomEnabled={true}
 ScrollEnabled={true}
 showsBuildings={true}
 showsMyLocationButton={false}/>
like image 246
Diksha Deep Avatar asked May 24 '18 08:05

Diksha Deep


2 Answers

animate(){
    let r = {
        latitude: 42.5,
        longitude: 15.2,
        latitudeDelta: 7.5,
        longitudeDelta: 7.5,
    };
    this.mapView.root.animateToRegion(r, 2000);
}

render(){
    return(
        <MapView
            ref = {(ref)=>this.mapView=ref}
            region={{
                latitude: 35.688442,
                longitude: 51.403753,
                latitudeDelta: 0.5,
                longitudeDelta: 0.5,
            }}
            onPress={()=>this.animate()}
        >

           ...markers...

        </MapView>
    );
}
like image 162
Mahdi Bashirpour Avatar answered Nov 07 '22 20:11

Mahdi Bashirpour


clickOnSearchedAddress = (placeId, index, dscrpt) => {
  getLatLongFromAddress(placeId).then((response) => {

    let r = {
      [Constants.KEY_LATITUDE]: response.geometry.location.lat,
      [Constants.KEY_LONGITUDE]: response.geometry.location.lng,
      latitudeDelta: latitudeDelta,
      longitudeDelta: longitudeDelta
    }
    this.mapView.animateToRegion(r, 1000)
    Keyboard.dismiss()
    isSetTextProgramatically = true;
    this.setState({
      search: dscrpt,
      searchData: [],

    })

  }).then((error) => { })
}


<MapView

  ref={ref => this.mapView = ref}
  provider={PROVIDER_GOOGLE}
  style={[styles.map, , { width: this.state.width }]}

  initialRegion={region}
  onRegionChangeComplete={this.onRegionChange}
  showsMyLocationButton={true}
  showsCompass={true}
  showsUserLocation={true}
  onMapReady={() => this.setState({ width: width - 1 })}}
/>
like image 33
anshuman burmman Avatar answered Nov 07 '22 21:11

anshuman burmman