React Native:标记静态工作,但不能动态工作

时间:2019-12-15 12:17:22

标签: react-native react-native-maps

我正在尝试在React Native Mapview中显示多个标记。我的代码

this.state = {
      coords: [
        { latitude: 23.759119, longitude: 90.411804 },
        { latitude: 23.759188, longitude: 90.41167 },
        { latitude: 23.759127, longitude: 90.41138 },
        { latitude: 23.759224, longitude: 90.411995 },
        { latitude: 23.7591, longitude: 90.41138 }
      ]}

当像这样的代码时可以正常工作

<Marker coordinate={this.state.coords[0]} />
          <Marker coordinate={this.state.coords[1]} />
          <Marker coordinate={this.state.coords[2]} />
          <Marker coordinate={this.state.coords[3]} />
          <Marker coordinate={this.state.coords[4]} />

但是在像这样的代码时不起作用

{this.state.coords.map((c, index) => {
            <Marker key={index} coordinate={c} />;
          })}

我没有收到任何错误。还不明白为什么不工作的代码。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我什么也没退。所以像这样修改代码

{this.state.coords.map((c, index) => {
            return <Marker key={index} coordinate={c} />;
          })}