我想在按下地图上的某个Markers时执行一些操作。我所拥有的:
1)一个MapView,如下所示:
<MapView
provider={PROVIDER_GOOGLE}
initialRegion={this.state.region}
style={styles.map}>
{this.displayRouteLocations(locations)}
</MapView>2)我的方法displayRouteLocations执行以下操作:
displayRouteLocations(locations) {
return locations.map((location, i) => {
return (
<PointMarker
key={i}
coordinate={location}
title={location.name}
description={location.description}
onPress={() => alert('test')}
/>
);
});
}3)最后,PointMarker是一个单独的组件,如下所示:
import {Marker} from 'react-native-maps';
class PointMarker extends Component {
render() {
return (
<Marker
{...this.props}
pinColor={'#f6c23d'}
tracksViewChanges={false}
/>
);
}
}我不知道为什么onPress不能在PointMarker上工作。我将其传递给react-native-maps Marker,但不知何故,当我按下标记时,它不会被触发。
我已经尝试在GitHub上的react-native-maps官方文档中搜索答案。在this example中,它们的作用大致相同(调用箭头函数onPress)。然而,它在我的情况下不起作用。
我正在Android上测试这个应用程序。
发布于 2020-02-24 07:18:31
根据文档,您应该将stopPropagation设置为true。如下所示:
displayRouteLocations(locations) {
return locations.map((location, i) => {
return (
<PointMarker
key={i}
coordinate={location}
title={location.name}
description={location.description}
onPress={(e) => {e.stopPropagation(); alert('test');}}
/>
);
});
}欲了解更多信息,请访问:<Marker /> Component API
更多信息2:Suggestion
https://stackoverflow.com/questions/60367759
复制相似问题