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

标签 android react-native react-native-maps mobile-development

我对原生 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/

相关文章:

Android - 如何将对象序列化和反序列化到公共(public)外部存储或从公共(public)外部存储中反序列化对象?

android - 绑定(bind)Android服务

javascript - 语法错误 : Strict mode does not allow function declarations in a lexically nested statement

react-native - 使用 Native-Base 在 FlatList 中关闭 SwipeRow

android - React Native Maps 和带有图像的标注

android - 应用程序不断停止 react 原生 map 并 react 原生火力基地

android - 如何将系统音量与媒体播放器应用程序同步

android - 如何在 Android 中使用 Navigation Drawer 设置 Navigation Component?

javascript - 无法正确 trim 和粗体匹配两个字符串中的单词

javascript - React-native-maps 标记方法 animateMarkerToCooperative 抛出未定义的错误