gpt4 book ai didi

android - react-native-maps:如何定期更新从远程服务器收到的用户位置

转载 作者:太空狗 更新时间:2023-10-29 13:46:49 25 4
gpt4 key购买 nike

我对原生 react 还很陌生,不知何故我能够在 map 上显示 map 和一些标记。但我需要从远程服务器读取一组位置(坐标)并显示在 map 上。换句话说,制造商需要改变他们的位置。

我尝试了几种不同的方法,但其中任何一种都没有帮助。如果有人有以前的经验,请帮助。

以下是我现有的代码。

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import MapView, {PROVIDER_GOOGLE} from 'react-native-maps';
import {Container, Header, Content, Footer, FooterTab, Title, Button, Icon} from 'native-base';

export default class App extends Component<Props> {
constructor(props) {
super(props);

this.state = {
latitude: 6.9212768,
longitude: 79.9610316,
error: null,
friends: [],
};
}

componentDidMount() {
navigator.geolocation.watchPosition(
(position) => {
console.log("wokeeey");
console.log(position);
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
//TODO: send user location to server
},
(error) => this.setState({error: error.message}),
{enableHighAccuracy: false, timeout: 200000, maximumAge: 1000},
);

//API call to get friends
this.setState({
friends: [
{
latitude: 6.9243768,
longitude: 79.9612316,
key: "friend 1"
},
{
latitude: 6.9213768,
longitude: 79.9641316,
key: "friend 2"
}
],
});
}

render() {
contents = this.state.friends.map((item) => {
return (
<MapView.Marker
key={item.key}
coordinate={{"latitude": item.latitude, "longitude": item.longitude}}
title={item.key}/>
);
});
return (
<Container>
<MapView
provider={PROVIDER_GOOGLE}
style={styles.container}
showsUserLocation={true}
showsMyLocationButton={true}
zoomEnabled={true}
followsUserLocation={true}
initialRegion={{
latitude: this.state.latitude,
longitude: this.state.longitude,
latitudeDelta: 0.0158,
longitudeDelta: 0.0070
}}
>
{!!this.state.latitude && !!this.state.longitude && <MapView.Marker
coordinate={{"latitude": this.state.latitude, "longitude": this.state.longitude}}
title={"You're here"} pinColor={'#3498db'}
/>}
<View>{contents}</View>
</MapView>
</Container>
);
}
}

最佳答案

您可以使用以下代码定期更新从远程服务器接收到的用户位置:

componentDidMount() {
setTimeout(function () {

// add your code for get and update makers every second

}, 1000);
}

关于android - react-native-maps:如何定期更新从远程服务器收到的用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52209104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com