【问题标题】:Marker not moving on map even after rerendering即使重新渲染后标记也不会在地图上移动
【发布时间】:2021-06-06 06:00:12
【问题描述】:

我每 5 秒从我的数据库中获取坐标,我的标记是动态的,这是我的渲染方法

console.log('render'+this.state.driverLocation.latitude +' '+this.state.driverLocation.longitude);


    let marker = null;
    marker = <MapView.Marker title='This is you' coordinate={this.state.driverLocation } />;

 return (
      <View style={styles.container}>
        <MapView
          initialRegion={this.state.focusedLocation}
          region={!this.state.locationChosen ? this.state.focusedLocation : null}
          style={styles.map}
          onPress={this.pickLocationHandler}
          ref={ref => this.map = ref}
        >
          {marker}
     


        </MapView>

render 中的日志语句显示 driverLocation 每 5 秒更改一次,但标记保持在初始位置(定义状态时给出的坐标)。

我的代码有问题吗?我需要添加一些东西吗?任何帮助将不胜感激。

【问题讨论】:

标签: react-native react-native-maps


【解决方案1】:

问题在于,android 仅依赖于更改键来更新自定义标记,并且可以通过在每次需要重新渲染时为标记分配一个随机键来解决。 所以,是这样的:

marker = <MapView.Marker 
title='This is you' 
coordinate={this.state.driverLocation}
 key={ this.GenerateRandomNumber() }
 } />;
 

同时:

GenerateRandomNumber=()=>
{
var RandomNumber = Math.floor(Math.random() * 100) + 1 ;
return RandomNumber;

}

【讨论】:

    猜你喜欢
    • 2020-02-18
    • 1970-01-01
    • 2019-03-05
    • 2022-11-04
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多