谁能告诉我,如何在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 },
);