【发布时间】:2019-01-19 14:02:15
【问题描述】:
我正在尝试学习如何使用 React Native 地图,并且我正在尝试添加自定义地图标记。出于某种原因,当我尝试使用以下代码时,图像会正确更新,但描述在第二次单击之前不会正确更新。第一次单击将显示“未选择”,但单击同一标记将显示我想要的实际文本。我该如何解决这个问题?
由于图像正在更新为 newImage,我知道 this.state.selectedMarkerIndex === i 但是由于某种原因,相同的相等性不适用于描述?
state = {busText:[]}
fetchData=(i, index)=>{
fetch('LINK TO GET BUSES'+ i.toString() + '/buses', {method:'GET'})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
selectedMarkerIndex: index,
busText: responseJson
},
()=>{
console.log("selected index: " + this.state.selectedMarkerIndex)
}
)
console.log(JSON.stringify(this.state.busText));
console.log("_______________________________________________________");
})
}
renderMarkers = ()=>{
return busStops.stops.map((stop, i) => {
return <Marker marker
key={ `${i}` }
coordinate={{latitude: stop.location.latitude, longitude: stop.location.longitude}}
title = {stop.stopName}
image = {this.state.selectedMarkerIndex === i ? newImage : busStopImage}
description={this.state.selectedMarkerIndex === i ? JSON.stringify(this.state.busText) : "not selected"}
onPress={() => {
this.fetchData(stop.stopID, i)
console.log(this.state.selectedMarkerIndex + "i :" + i)
}
}
/>
})
}
我希望 MapMarker 的描述在我单击它时会更新,其中包含已获取但未发生的内容。
【问题讨论】:
标签: reactjs react-native