Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render multiple marker in react-native-maps

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

like image 410
Hazim Ali Avatar asked Nov 11 '16 04:11

Hazim Ali


1 Answers

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>
like image 176
vinayr Avatar answered Oct 01 '22 18:10

vinayr