Hye, Im new in react native and want to ask how do I render multiple markers in maps.
This is my code
inside class:-
constructor(props) {
super(props);
this.state = {
coordinate: ([{
latitude: 3.148561,
longitude: 101.652778,
title: 'hello'
},
{
latitude: 3.149771,
longitude: 101.655449,
title: 'hello'
}
]),
};
}
inside render:-
<MapView
style={styles.map}
showsUserLocation={true}
followUserLocation={true}
zoomEnabled={true}
//annotations={markers}
>
<MapView.Marker
coordinate={this.state.coordinate}
title={this.state.coordinate.title}
/>
</MapView>
I want to render this two marker inside maps and I have no idea how to make loop yet in react native to render this. I already try what in documentation but still not working.
Thank you in advance :)
coordinate
property is not constructed properly. Do something like this -
this.state = {
markers: [{
title: 'hello',
coordinates: {
latitude: 3.148561,
longitude: 101.652778
},
},
{
title: 'hello',
coordinates: {
latitude: 3.149771,
longitude: 101.655449
},
}]
}
Inside render
<MapView
....
>
{this.state.markers.map(marker => (
<MapView.Marker
coordinate={marker.coordinates}
title={marker.title}
/>
))}
</MapView>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With