首页 > 解决方案 > 使用 forEach 显示来自 React Native Maps 的标记

问题描述

我的状态中有这个数组:

coinCoordinatesArray: [
        {
        latitude: 37.78825,
        longitude: -122.4324},
        {
        latitude: 38.78825,
        longitude: -120.4324},
        {
        latitude: 31.78825,
        longitude: -80.4324},
        {
        latitude: 55.78825,
        longitude: -95.4324}

我正在尝试使用该州的坐标来渲染标记:


            {this.state.coinCoordinatesArray.forEach((coordinate) => {
                        return  (<MapView.Marker
                          onPress={() => alert('fire')}
                          icon={require('./assets/coin_small.png')}
                          coordinate={coordinate}
                          />)
            })}

然而,标记根本没有呈现,但我也没有得到任何错误。

标签: javascriptarraysreact-nativeobjectecmascript-6

解决方案


您正在执行 a forEach(),它只生成元素,您可能想要执行 a map(),因此您的项目被累积到一个数组中并被渲染。


推荐阅读