Search code examples
react-nativereact-native-maps

How do I render a button inside the react-native-maps pop-up box?


I am trying to render a button which I can use to redirect to another page within the description page (the pop-up-box if you select a marker) using AirBnb maps (using MapView.Markers). I am not sure how to achieve this.

Currently my markers look as follow:

const markers = [{
  longitude: 8.545592,
  latitude: 47.366465,
  description: "Bike 1",
  title: "Bike 1"
},
{
  longitude: 8.545892,
  latitude: 47.366365,
  description: "Bike 2",
  title: "Bike 2"
}
];

Whenever I try to input JSX into a text, I get a NSMutableDictionary cannot be converted to NSString error. (When I do this:

const markers = [{
  longitude: 8.545592,
  latitude: 47.366465,
  description: <Text>Bike1</Text>, //"Bike 1", //<Text> Bike1 </Text>
  title: "Bike 1"
},
{
  longitude: 8.545892,
  latitude: 47.366365,
  description: "Bike 2",
  title: "Bike 2"
}
];

Solution

  • For more than just text, what you should do is define a custom Callout instead of using description prop. For example:

    <MapView.Marker
      coordinate={{longitude: marker.longitude, latitude: marker.latitude}}
      title={marker.title}
    >
      <MapView.Callout>
        <View style={styles.callout}>
          <Button title='Click Me!' onPress={() => console.log('Clicked')} />
        </View>
      </MapView.Callout>
    </MapView.Marker>