问题描述
我正在尝试向地图添加标记。我想将所有位置数据保存在一个单独的文件中,因此我试图从data.json文件中提取坐标。 latlng坐标位于json文件的数组中。我使用的位置属性不正确吗?
render() {
return (
<View style={styles.container}>
<DestinationButton />
<CurrentLocationButton
cb={() => {
this.centerMap();
}}
/>
<MapView
initialRegion={this.state.region}
showsUserLocation={true}
showsCompass={true}
rotateEnabled={false}
ref={(map) => {
this.map = map;
}}
style={{ flex: 1,zIndex: 0 }}
>
{data.features.map((tc) => (
<Marker
key={tc.properties.NAME}
position={{
lat: tc.geometry.coordinates[1],lng: tc.geometry.coordinates[0],}}
/>
))}
</MapView>
</View>
);
}
解决方法
似乎您为<Marker/>
提供了错误的道具,应该是坐标而不是位置。根据文档:https://github.com/react-native-maps/react-native-maps#rendering-a-list-of-markers-on-a-map
<Marker
key={tc.properties.NAME}
coordinate={{
latitude: tc.geometry.coordinates[1],longitude: tc.geometry.coordinates[0],}}
/>