【问题标题】:React native Airbnb Markers: Markers successfully disappear, but do not re-appearReact native Airbnb Markers:标记成功消失,但不再出现
【发布时间】: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


    【解决方案1】:

    我找到的当前解决方案如下(我知道它非常不雅,而且我知道它可能真的不安全,因为碰撞箱可能仍然存在[碰撞箱仍然存在吗?]),但只是渲染标记并通过隐藏它们将半径设为零可以让我实现这一目标。

    所以我有以下标记渲染:

    return (
                <View key={marker.bike_no}>
                { <MapView.Marker
                navigator={this.props.navigator}
                coordinate={{longitude: marker.longitude, latitude: marker.latitude}}
                title={marker.title}
                description={marker.description}
                onPress={(coord, pos) => this.startBookingBike(marker.bike_no)}
                key={marker.key}
                ><View style={(marker.cur_user == 0) ? styles.bikeRadius : styles.markerStyleHidden }><View style={styles.bikeMarker}>
                </View></View>
                </MapView.Marker>
              }</View>
              );
    

    使用此处定义的样式属性:

    markerStyleHidden: {
        height: 0,
        width: 0,
        borderRadius: 0,
        overflow: 'hidden',
        backgroundColor: 'rgba(0, 122, 255, 0.5)',
        borderWidth: 0,
        borderColor: 'rgba(0, 122, 255, 0.5)',
        alignItems: 'center',
        justifyContent: 'center'
      },
      radius: {
        height: 30,
        width: 30,
        borderRadius: 30 / 2,
        overflow: 'hidden',
        backgroundColor: 'rgba(0, 122, 255, 0.5)',
        borderWidth: 4,
        borderColor: 'rgba(0, 122, 255, 0.5)',
        alignItems: 'center',
        justifyContent: 'center'
      },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      相关资源
      最近更新 更多