我在使用json文件提取的mapview标记时出现问题。如果我从文件中导入它,我可以有自定义标记,但我需要从json导入坐标。我在componentDidMount中从setState获取我当前的位置没有问题,但似乎它与标记的工作方式不同。我正在测试日志记录,似乎渲染中的前两轮数组是空的,但由于我在同一个地方设置initialRegion,我不明白这个问题? 这是我的componentDidMount的一些(部分):
return fetch('https://url.json')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
data : responseJson,
});
})
.catch((error) =>{
console.error(error);
});
这是我MapView的一部分:
<MapView style={styles.map}
initialRegion={{
latitude:this.state.latitude,
longitude:this.state.longitude,
latitudeDelta: 0.043,
longitudeDelta: 0.034
}}
ref={c => this.mapView = c}
onPress={this.onMapPress}
loadingEnabled={true}
>
{this.state.data.map(marker => (
<MapView.Marker
key={marker.name}
coordinate={{
latitude: marker.lat,
longitude: marker.lng,
}}
image={img}
title={marker.title}
description={marker.description}
/>
))}