【发布时间】:2017-11-25 17:11:34
【问题描述】:
我目前正在使用 react-native 构建一个应用程序,并且正在使用 Airbnb 地图插件来动态显示标记。我的标记已成功从 firebase 数据库下载,并且下载的数组始终显示正确的内容。我这里有完整的代码以防万一https://codepen.io/yeni/pen/Kqyrxv?editors=0010 :)
每当我将标记的cur_user 属性更改为1 时,它都会成功地不显示在地图上。但是,当我将cur_user 更改回0 时,不会显示标记(这将是所需的行为)。我的代码如下所示:
//Store file MarkersStore.js
class MarkersStore {
@observable markers = [];
constructor() {
console.log("Started downloading markers");
var localMarkers = [];
Fb.bikes.on('value', (snap) => {
localMarkers = [];
snap.forEach((marker) => {
localMarkers.push({
longitude: parseFloat(marker.val().positionLng),
latitude: parseFloat(marker.val().positionLat),
description: "Bike 1",
title: "b" + String(marker.val().bike_no),
bike_no: marker.val().bike_no,
cur_user: marker.val().current_user,
key: marker.getKey()
})
});
//Once must make sure that the upper command already finished executing!
this.markers.replace(localMarkers);
console.log("Loaded markers");
});
}
}
...
//Rendering file MarkersComponent.js
render() {
var renderingMarkers = this.fbMarkers.markers.slice() || [];
console.log("fbMarkers are: ");
console.log(this.fbMarkers.markers.slice());
console.log("Rendering stuff..");
console.log(renderingMarkers);
console.log("Rendering ALL the stuff!");
return (<View>
{renderingMarkers.map( (marker) => {
console.log("Markers are re-rendered");
console.log(marker.bike_no);
console.log(marker.cur_user);
//All console logs up until here show correct results
return (
<View key={marker.bike_no}>{ (marker.cur_user == 0) ?
<MapView.Marker
navigator={this.props.navigator}
key={marker.bike_no}
coordinate={{longitude: marker.longitude, latitude: marker.latitude}}
title={marker.title}
description={marker.description}
onPress={(coord, pos) => this.startBookingBike(marker.bike_no)}
><View style={styles.bikeRadius}><View style={styles.bikeMarker}>
</View></View>
</MapView.Marker> : null
}</View>
);
})}</View>)
}
此外,每次我更改数据库中的某些数据时都会成功触发自动运行(并显示正确的检索数据)。有没有办法将自动运行功能放入渲染方法中以成功重新渲染所有内容?
** 编辑 ** 我还在官方 github 页面上打开了一个问题,因为这似乎是一个错误。如果我错了纠正我! :) https://github.com/airbnb/react-native-maps/issues/1426
【问题讨论】:
标签: javascript google-maps react-native jsx react-native-android