如何访问 react-native-maps MapView 的所有 MapView.Markers?

Jon*_*ulf 5 react-native react-native-maps

在使用 react-native-maps 的 react-native 应用程序中,我试图以编程方式显示许多中MapView.Callout特定MapView.Marker的 。我打算使用 showCallout 方法,但还没有找到如何访问 MapView 的所有标记,我可以从那里根据它的 id/key/ref 选择正确的标记。

标记在地图循环中渲染到 MapView 上,如下所示。

到目前为止,我尝试使用 this.refs.mapView.refs / this.refs.mapView.children 来获取所有 MapView 的标记但没有成功,但我在那里什么也没得到。

<MapView>
    ref={'mapView'}
    ...
>
    {this.props.screenProps.appState.cachedDeviations.map(deviation => {
        return (
            <MapView.Marker
                coordinate={
                    deviation.position
                }
                key={deviation.Id}
                ref={deviation.Id}
            >
                <MapView.Callout style={styles.callout}>
                    <DeviationCallout
                        ...
                    />
                </MapView.Callout>
            </MapView.Marker>
        )
    })
}
</MapView>
Run Code Online (Sandbox Code Playgroud)

任何提示?

ben*_*nel 6

您可以使用功能参考。

例子

<MapView.Marker
    coordinate={ deviation.position }
    key={deviation.Id}
    ref={(ref) => this.markers[deviation.Id] = ref}
>
  // ...
</<MapView.Marker>

// ...

this.markers[someId].showCallout();
Run Code Online (Sandbox Code Playgroud)