javascript - 在 React Native Maps 中的组件上使用方法

标签 javascript react-native react-native-maps

我正在尝试在 React Native Maps 中的标记上使用 animateMarkerToCooperative 方法。

文档提出以下建议:

componentWillReceiveProps(nextProps) {
  const duration = 500

  if (this.props.coordinate !== nextProps.coordinate) {
    if (Platform.OS === 'android') {
      if (this.marker) {
        this.marker._component.animateMarkerToCoordinate(
          nextProps.coordinate,
          duration
        );
      }
    } else {
      this.state.coordinate.timing({
        ...nextProps.coordinate,
        duration
      }).start();
    }
  }
}

render() {
  return (
    <MapView initialRegion={...}>
      <Marker.Animated
        ref={marker => { this.marker = marker }}
        coordinate={this.state.coordinate}
      />
    </MapView>
  );
}

我尝试在我的代码中实现这一点。文档中的代码是用古老的React编写的(不推荐使用componentWillReceiveProps)

<MapView.Marker.Animated draggable
                coordinate={this.state.playerMarkerPositionFuture}
                title={"Player"}
                description={"Player marker!"}
                image={require('./assets/player_map_icon_small_transparent.png')}
                onDragEnd={(event) => this.setState({ playerMarkerPositionFuture: event.nativeEvent.coordinate })}
                onDragStart={this.movePlayerMarker()}
                />

...

movePlayerMarker = () => {
    this.marker._component.animateMarkerToCoordinate(
      nextProps.coordinate,
      1000
    );
  }

但是我收到此错误:

undefined is not an object (evaluating '_this.marker._component')

找到一个更好的例子here .

最佳答案

从动画标记方法中删除_component。只需编写以下内容

this.marker.animateMarkerToCoordinate(
          nextProps.coordinate,
          duration
        );

而不是

this.marker._component.animateMarkerToCoordinate(
          nextProps.coordinate,
          duration
        );

它会起作用。

关于javascript - 在 React Native Maps 中的组件上使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56043070/

相关文章:

reactjs - 如何根据是否选择动态更新 MapMarker 图像集合?

javascript - react native : Download Remote Image to local and use static url

Javascript - 显示调用函数名称的通用函数

android - 在 native react 中忽略 SSL 证书验证

javascript - map API v3 : Calculate bearing

react-native - 获取路线 API 调用? react 原生

javascript - 如何在 react native Realm 中存储 [String] 或 [Int]

ubuntu - 构建时运行 android react native maps 失败

javascript - 我应该将哪个效果库与 GWT 集成?

c# - 单元测试 : How to check if generated javascript (string) is valid javascript