3

Hye,Im新的反应本地化并想问我如何在地图中渲染多个标记。在react-native-maps中渲染多个标记

这是我的代码

内部类: -

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> 

我想渲染里面的地图和我这两个标记不知道如何在原生反应中做出循环来渲染它。我已经尝试了文档中的内容,但仍然无法工作。

预先感谢您:)

回答

14

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> 
+1

谢谢!在此之前,我误解了如何构建坐标的属性,因为我遵循react-native的MapView。再次感谢。你真的帮助我很多:)有一个美好的一天^ _^ –

+1

出于某种原因,这个确切的代码不适合我。 –

+0

对我来说,我不得不添加一个'index'和'key'。 '{this.state.markers.map((标记指数)=>( \t \t \t \t \t \t \t \t \t \t))}'' –