Reputation: 250
I'm trying to display markers on my MapView. I've followed the example, but none of the markers are displaying on the map, although I can center the map and the location indicator displays. I've imported MapView and Marker from react-native-maps. Any help would be appreciated.
constructorprops: any {
super(props);
this.state = {
region: defaultRegion,
markers: [
{
coordinate: {
latitude: 37.298984,
longitude: -122.050362
},
title: "Best Place",
description: "Description1",
id: 1
},
{
coordinate: {
latitude: 37.297803,
longitude: -122.050037
},
title: "Best Place2",
description: "Description 2",
id: 2
}
]
};
}
centerLocation = () => {};
componentDidMount() {
this.centerLocation();
}
render() {
return (
<MapContainer>
<MapView
style={styles.map}
showsUserLocation={true}
region={this.state.region}
/>
<CenterButton centerLocation={this.centerLocation} />
{this.state.markers.map((marker: any) => (
<Marker
key={marker.id}
coordinate={marker.coordinate}
title={marker.title}
description={marker.description}
/>
))}
</MapContainer>
);
}
}
Upvotes: 3
Views: 18019
Reputation: 887
your Marker tag should be within the MapView tag, you have closed the MapView tag before putting your Marker tag inside for example
<MapView style={styles.map} showsUserLocation={true} region={this.state.region}>
{this.state.markers.map((marker: any) => (
<Marker
key={marker.id}
coordinate={marker.coordinate}
title={marker.title}
description={marker.description}
/>
))}
</MapView>
Upvotes: 9
Reputation: 2870
Please add below code in render function, hope it will help you
return (
<View>
<MapView
ref={MapView => (this.MapView = MapView)}
style={styles.map}
initialRegion={this.state.region}
loadingEnabled = {true}
loadingIndicatorColor="#666666"
loadingBackgroundColor="#eeeeee"
moveOnMarkerPress = {false}
showsUserLocation={true}
showsCompass={true}
showsPointsOfInterest = {false}
provider="google">
{this.state.markers.map((marker:any) => (
<MapView.Marker
key={marker.id}
coordinate={marker.coordinate}
title={marker.title}
description={marker.description}
/>
}
</MapView>
<CenterButton
centerLocation={this.centerLocation} />
</View>
);
Upvotes: 9