【问题标题】:Render multiple marker in react-native-maps在 react-native-maps 中渲染多个标记
【发布时间】:2016-11-11 04:25:44
【问题描述】:

嘿,我是 react native 的新手,想问一下如何在地图中渲染多个标记。

这是我的代码

类内:-

constructor(props) {
super(props);

  this.state = {
   coordinate: ([{
     latitude: 3.148561,
     longitude: 101.652778,
     title: 'hello'
   },
   {
     latitude: 3.149771,
     longitude: 101.655449,
     title: 'hello'
   }
  ]),
 };

}

内部渲染:-

<MapView
        style={styles.map}
        showsUserLocation={true}
        followUserLocation={true}
        zoomEnabled={true}
        //annotations={markers}
      >  
            <MapView.Marker
              coordinate={this.state.coordinate}
              title={this.state.coordinate.title}
            />
      </MapView>

我想在地图内渲染这两个标记,但我不知道如何制作循环以响应本机来渲染它。我已经尝试了文档中的内容,但仍然无法正常工作。

提前谢谢你:)

【问题讨论】:

    标签: javascript react-native google-maps-markers react-native-android react-native-ios


    【解决方案1】:

    coordinate 属性构造不正确。做这样的事情 -

    this.state = {
      markers: [{
        title: 'hello',
        coordinates: {
          latitude: 3.148561,
          longitude: 101.652778
        },
      },
      {
        title: 'hello',
        coordinates: {
          latitude: 3.149771,
          longitude: 101.655449
        },  
      }]
    }
    

    内部渲染

    <MapView 
      ....
    >
      {this.state.markers.map(marker => (
        <MapView.Marker 
          coordinate={marker.coordinates}
          title={marker.title}
        />
      ))}
    </MapView>
    

    【讨论】:

    • 谢谢!在此之前,当我从 react-native 遵循 MapView 时,我误解了如何构造坐标的属性。再次感谢。你真的帮了我很多:)祝你有美好的一天^_^
    • 由于某种原因,这个确切的代码对我不起作用。
    • 对我来说,我必须添加一个indexkey{this.state.markers.map((marker, index) =&gt; ( &lt;MapView.Marker key={index} coordinate={marker.coordinates} title={marker.title} /&gt; ))}
    • 解决了多标记问题。谢谢。
    猜你喜欢
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2016-10-21
    • 2021-11-12
    • 2018-07-16
    • 2022-08-22
    相关资源
    最近更新 更多