【问题标题】:How to create live tracking in react native?如何在 React Native 中创建实时跟踪?
【发布时间】:2020-02-11 19:05:10
【问题描述】:

谁能告诉我,如何在 react native 中创建实时跟踪?使用地理定位,反应原生地图或其他依赖项,我想做一个像 uber 一样的跟踪驱动程序

【问题讨论】:

    标签: react-native google-maps geolocation maps tracking


    【解决方案1】:

    希望您已安装 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 },
    );
    

    【讨论】:

    • 一旦开始跟踪,地图是否会根据方向线旋转以跟随方向?比如谷歌地图实时路线
    • 它不会@MUSTAPHAGHLISSI,您可以使用航向来获取方向并相应地旋转地图。
    • 能否提供参考或任何帮助链接?
    • 嘿,你能解释一下你的代码中的“导航器”是什么吗?我见过这段代码的例子,但这个变量从未被导入或创建
    • @Kailash 虽然很慢,但是当用户移动时,为当前用户位置渲染标记视图非常慢。这个问题有什么解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多