我正在写一个跟踪用户位置的web应用程序。我想在浏览器中测试更改地理位置是否会实时更新UI。当你使用谷歌地图和物理移动地图时,地图会相应地更新。
我遇到了Chrome地理定位传感器,但它不能实时更新位置。我需要刷新整个页面才能更改位置
有没有人知道我怎么才能实时完成这件事?
我正在使用google-maps-react
发布于 2019-07-22 09:34:46
您可以利用JS计时器为您完成此操作。使用setInterval,只需添加检查用户的GPS位置的功能。
setInterval(
function(){
navigator.geolocation.getCurrentPosition(function(position) {
this.setState({pos: position.coords});
});
},
3000);之后,您可以使用this.state.pos.latitude和this.state.pos.latitude在状态更改时实时更新地图。顺便说一句,这每3秒更新一次用户的位置。
发布于 2019-07-22 18:16:46
好了,我想通了!我使用react-geolocated并将watchPosition设置为true。它支持实时跟踪,只要我更改Chrome地理位置传感器,它就会更新:-)
https://stackoverflow.com/questions/57138143
复制相似问题