如何在 React Native 中创建实时跟踪?

How to create live tracking in react native?

谁能告诉我,如何在 React Native 中创建实时跟踪?使用地理定位,反应本地地图或其他依赖项,我想制作一个像 uber 这样的跟踪驱动程序

希望您已经安装了 react-native-maps 并将必要的包添加到您的应用程序中。在 componentDidMount() 中使用下面给出的代码。只要您的位置发生变化,就会调用下面给出的代码。它将为您提供更新的位置。在这里您还可以找到航向,您可以使用航向来显示车辆方向或路线。

this.watchId = navigator.geolocation.watchPosition(
  (position) => {
    this.locationPermission = true;
    console.log('Watch Position response', position);
    const { coordinate } = this.state;
    const { latitude, longitude } = position.coords;
    const newCoordinate = {
      latitude,
      longitude
    };
    if (Platform.OS === 'android') {
      if (this.marker) { this.marker._component.animateMarkerToCoordinate(newCoordinate, 500); }
    }
    else {
      coordinate.timing(newCoordinate).start();
    }

    this.setState({
      marker: {
        latlng: {
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        }
      },
      heading: position.coords.heading,
    })


    );

  },
  (error) => this.setState({ error: error.message }),
  { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000, distanceFilter: 10 },
);