javascript - OnPress 不适用于 react native map 中的标记

标签 javascript react-native react-native-maps

我想在按下 map 上的标记之一时执行一些操作。我有什么:

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它们的作用几乎相同(在 Press 上调用箭头函数)。但是,它不适用于我的情况。

我正在 Android 上测试该应用。

最佳答案

根据文档,您应该设置 stopPropagation为真。像这样:

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

关于javascript - OnPress 不适用于 react native map 中的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60367759/

相关文章:

javascript - 如何拦截并获取重定向 URL header 中的位置?

javascript - 在新位置更改时更新 map 上的标记位置 - React Native

javascript - 切换菜单不会在导航单击时关闭

javascript - 从 React Native 将图像上传到 firebase

react-native - ID 为 2 的动画节点已存在

ios - 我的 native 应用程序在Swift中编译器生成的崩溃

ios - 无法从 React Native map 触发 React 导航

google-maps - 你可以在 react 原生 map 中自定义当前位置标记吗?

javascript - Google关闭: Centralized AJAX 'decoder' ?

Javascript 在 Canvas 旋转时查找像素