我正在尝试在MapView上显示标记。我遵循了该示例,但是虽然我可以将地图居中并显示位置指示器,但所有标记都没有显示在地图上。我已经从react-native-maps导入了MapView和Marker。任何帮助将不胜感激。
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>
);
}
}
答案 0 :(得分:2)
请在render函数中添加以下代码,希望对您有所帮助
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>
);
答案 1 :(得分:0)
您的Marker标记应位于MapView标记内,例如,在将Marker标记放入内部之前,您已经关闭了MapView标记
<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>