Render multiple marker in react-native-maps

Hazim Ali picture Hazim Ali · Nov 11, 2016 · Viewed 23.3k times · Source

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 :)

Answer

vinayr picture vinayr · Nov 11, 2016

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>