Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native-Maps: How to animate to coordinate?

I am using the following code with no success:

//Inside of NativeBase Container

        <MapView.Animated
           ref="map"
           style = {styles.map}
           showsUserLocation = {true}
           zoomEnabled = {true}
           showsMyLocationButton = {true}
           showsCompass = {true}
           showScale = {true}
           showsIndoors = {true}
           mapType = {this.state.mapTypes[this.state.mapTypeCode]}
        />

//Inside of componentDidMount of the class

navigator.geolocation.getCurrentPosition(
  (position) => {
    var initialPosition = JSON.stringify(position.coords);
    this.setState({position: initialPosition});

    let tempCoords = {
      latitude: Number(position.coords.latitude),
      longitude: Number(position.coords.longitude)
    }

    this.refs.map.animateToCoordinate(tempCoords, 1);

  }, function (error) { alert(error) },

);

But an error occurs saying there is no such function animateToCoordinate.

like image 738
Diego Penteado Avatar asked Feb 03 '17 19:02

Diego Penteado


2 Answers

Unfortunately now animateToCoordinate is deprecated and if you want to do the same, you should use animateCamera or animateToRegion instead.

render() {
  return ( 
    <MapView style = {styles.maps}
      ref = {(mapView) => { _mapView = mapView; }}
      initialRegion = {{
        latitude: 6.8523,
        longitude: 79.8895,
        latitudeDelta: 0.0922,
        longitudeDelta: 0.0421,
      }}
    />
    <TouchableOpacity 
       onPress = {() => _mapView.animateToRegion({
        latitude: LATITUDE,
        longitude: LONGITUDE
      }, 1000)}>
      <Text>Tap</Text>
    </TouchableOpacity>
  )

}

like image 124
Masoud Avatar answered Sep 24 '22 18:09

Masoud


In my case I used animateToCoordinate() like follows and it works for me:

  var _mapView: MapView;

  render() {
      return ( 
        <MapView style = {styles.maps}
          ref = {(mapView) => { _mapView = mapView; }}
          initialRegion = {{
            latitude: 6.8523,
            longitude: 79.8895,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }}
        />
        <TouchableOpacity 
           onPress = {() => _mapView.animateToCoordinate({
            latitude: LATITUDE,
            longitude: LONGITUDE
          }, 1000)}>
          <Text>Tap</Text>
        </TouchableOpacity>
      )
  }
like image 22
Dinith Minura Avatar answered Sep 25 '22 18:09

Dinith Minura