I'm experimenting with React, and I want to render a few markers on a map (I'm using Google Maps API). Now, everything is fine if I hardcode the markers (in the example, 5 markers, each with different coordinates, name and description, as in the locations array below). But what if i want to loop through the array and render them without hardcoding at all? I defined the renderMarkers function before the render(). Any help would be appreciated. Thanks!
/* Main component state */
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
mapReady: true,
desc: '',
animation: null,
locations:
[
{
"locationName": "name1",
"position": '{"lat": "lat1", "lng": "lng1"}',
"desc": "desc1"
},
{
"locationName": "name2",
"position": '{"lat": "lat2", "lng": "lng2"}',
"desc": "desc2"
},
{
"locationName": "name3",
"position": '{"lat": "lat3", "lng": "lng3"}',
"desc": "desc3"
},
{
"locationName": "name4",
"position": '{"lat": "lat4", "lng": "lng4"}',
"desc": "desc4."
},
{
"locationName": "name5",
"position": '{"lat": "lat5, "lng": "lng5"}',
"desc": "desc5."
}
]
};
/* Function to render the markers, each with their relevant info taken from the state.locations array, on the map */
renderMarkers = () => {
for (let i = 0; i < this.state.locations.length; i++) {
return <Marker
onClick = { this.onMarkerClick }
title = { this.state.locations[i].locName }
position = { JSON.parse(this.state.locations[i].position) }
desc = { this.state.locations[i].desc }
animation = { this.state.animation[i] }
name = { this.state.locations[i].locName } />
}
}