如何解决即使重新渲染后标记也不会在地图上移动
我每 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 秒更改一次,但标记保持在初始位置(定义状态时给出的坐标)。
我的代码有问题吗?我需要添加一些东西吗?任何帮助将不胜感激。
解决方法
问题在于,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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。