【发布时间】:2020-06-07 14:26:19
【问题描述】:
我想在地图上按下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 上测试该应用。
【问题讨论】:
-
你试过onSelect吗?
-
@EugenSunic,是的,没有成功。另外,
onSelect仅适用于 iOS,我正在 Android 上进行测试。 -
onPress={(e) => {e.stopPropagation(); alert(''某事)}
-
还是不行。这种方法也试过了。
-
另外,在我在问题中提到的文档示例中,他们不使用 stopPropagation。
标签: javascript react-native react-native-maps