0

I'm new to react and js and the whole 'state' concept. Anyways, I'm trying to loop through an array of car dealers with the value lat, lng and boolean (open or not). This is what the code looks like now and I'm wondering how the MapView.Marker code should look for that to work. Thanks

  <MapView style={styles.map}
    region ={{
    latitude:40.758927,
    longitude:-73.984981,
    latitudeDelta: 0.1,
    longitudeDelta: 0.1,
 }}
 >
<MapView.Marker
  coordinate={{
    latitude:40.758927,
    longitude:-73.984981,
  }}
    title={'Ferrari dealer'}
    description={'Currently closed'}
    />

    </MapView>

1 Answer 1

3

Something like this:

let markers = this.state.cardDealers.map(dealer => (
  <MapView.Marker
    key={dealer.id}
    coordinate={{
      latitude: dealer.lat,
      longitude: dealer.lng,
    }}
    title={dealer.title}
    description={dealer.open ? 'Open' : 'Currently closed'}
  />
));

return (
  <MapView style={styles.map}
      region ={{
      latitude:40.758927,
      longitude:-73.984981,
      latitudeDelta: 0.1,
      longitudeDelta: 0.1,
   }}
  >
    {markers}
  </MapView>
)

You might find React documentation on lists and keys helpful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.