javascript – React-Native-Maps:如何设置动画来协调?

前端之家收集整理的这篇文章主要介绍了javascript – React-Native-Maps:如何设置动画来协调?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码但没有成功:

//在NativeBase容器内部

  1. <MapView.Animated
  2. ref="map"
  3. style = {styles.map}
  4. showsUserLocation = {true}
  5. zoomEnabled = {true}
  6. showsMyLocationButton = {true}
  7. showsCompass = {true}
  8. showScale = {true}
  9. showsIndoors = {true}
  10. mapType = {this.state.mapTypes[this.state.mapTypeCode]}
  11. />

//在类的componentDidMount内部

  1. navigator.geolocation.getCurrentPosition(
  2. (position) => {
  3. var initialPosition = JSON.stringify(position.coords);
  4. this.setState({position: initialPosition});
  5.  
  6. let tempCoords = {
  7. latitude: Number(position.coords.latitude),longitude: Number(position.coords.longitude)
  8. }
  9.  
  10. this.refs.map.animateToCoordinate(tempCoords,1);
  11.  
  12. },function (error) { alert(error) },);

但是发生了一个错误,说没有这样的函数animateToCoordinate.

解决方法

我有一些问题,其中this.refs是未定义的,除非我在构造函数中将对此的引用绑定到我使用this.refs的函数.在你的情况下,试试这个:
  1. constructor(props) {
  2. super(props);
  3. this._getCoords = this._getCoords.bind(this);
  4. this.state = {
  5. position: null
  6. };
  7. }
  8.  
  9. componentDidMount() {
  10. this._getCoords();
  11. }
  12.  
  13. _getCoords = () => {
  14. navigator.geolocation.getCurrentPosition(
  15. (position) => {
  16. var initialPosition = JSON.stringify(position.coords);
  17. this.setState({position: initialPosition});
  18. let tempCoords = {
  19. latitude: Number(position.coords.latitude),longitude: Number(position.coords.longitude)
  20. }
  21. this._map.animateToCoordinate(tempCoords,1);
  22. },);
  23. };
  24.  
  25. render() {
  26. return (
  27. <MapView.Animated
  28. ref={component => this._map = component}
  29. />
  30. );
  31.  
  32. }

虽然仍然可以使用字符串引用,但我相信这是遗留的,所以我也将MapView引用更新为更新的方式.见:Ref Example

猜你在找的JavaScript相关文章