【发布时间】:2020-02-11 19:05:10
【问题描述】:
谁能告诉我,如何在 react native 中创建实时跟踪?使用地理定位,反应原生地图或其他依赖项,我想做一个像 uber 一样的跟踪驱动程序
【问题讨论】:
标签: react-native google-maps geolocation maps tracking
谁能告诉我,如何在 react native 中创建实时跟踪?使用地理定位,反应原生地图或其他依赖项,我想做一个像 uber 一样的跟踪驱动程序
【问题讨论】:
标签: react-native google-maps geolocation maps tracking
希望您已安装 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 },
);
【讨论】: