我正在尝试在React Native Maps的标记上使用animateMarkerToCoordinate方法。
文档提出以下建议:
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。
答案 0 :(得分:1)
从动画标记方法中删除_component。只需编写以下
this.marker.animateMarkerToCoordinate(
nextProps.coordinate,
duration
);
代替
this.marker._component.animateMarkerToCoordinate(
nextProps.coordinate,
duration
);
它会起作用。
答案 1 :(得分:0)