我正在制作一个带有react-native的应用程序,该应用程序需要根据用户移动的距离来更新用户的位置。我正在使用navigator.geolocation.watchPosition
作为事件监听器,但是行为不如预期。
这是我的测试代码:-
/**
* Location Tracking Service
*/
import Toast from "react-native-simple-toast";
// constants
const TIME_TO_CALL_API = 30000;
const WATCH_LOCATION_TIMEOUT = 3000;
const GeolocationOptions = {
enableHighAccuracy: isIOS ? true : false,
timeout: 20000,
maximumAge: 100
}
class LocationTrackingService {
static classInstance = null;
isRunningService = false;
/**
* @returns {LocationTrackingService}
*/
static getInstance() {
if (LocationTrackingService.classInstance == null) {
LocationTrackingService.classInstance = new LocationTrackingService();
}
return this.classInstance;
}
/**
* This Method Start Service In Background Mode
* If It Set Callback No Longer Work and UI no longer update
*/
startServiceInBackground() {
this.isServiceInBackground = true;
}
/**
* Method To Clock In The Provicer
*/
clockInProvider() {
navigator.geolocation.getCurrentPosition(response => {
if (response && response.coords) {
this.saveProviderClockInLogs(response.coords); // api method to save clock in logs
this.start();
}
},
(error) => {
console.log(error)
},
GeolocationOptions
)
}
/**
* Method To Clock Out Provier
*/
clockOutProvider() {
navigator.geolocation.getCurrentPosition((response) => {
if (response && response.coords) {
this.saveProviderClockOutLogs(response.coords); // api method to save clock out logs
}
},
(error) => {
console.log(error)
},
GeolocationOptions
);
}
providerRouteCoordinates = []; // provider routes followed coordinates
/**
* Method To Start Location Tracking
*/
start(destinationLocation) {
this.isRunningService = true;
let currentPosition = {
lat: '',
lng: ''
}
this.watchId = navigator.geolocation.watchPosition((response) => {
currentPosition.lat = this.convertToDecimalPoints(response.coords.latitude);
currentPosition.lng = this.convertToDecimalPoints(response.coords.longitude);
this.providerRouteCoordinates.push(`${(currentPosition.lat).toString()},${(currentPosition.lng).toString()}`);
console.log(this.providerRouteCoordinates);
if (this.calculateBookingLocationDistanceFromProviderLocation(currentPosition.lat, currentPosition.lng, destinationLocation.lat, destinationLocation.lng) <= 100) {
this.saveProviderRouteCoordinates(this.providerRouteCoordinates);
console.log('Provider Reached The Destination!')
} else {
this.saveProviderRouteCoordinates(this.providerRouteCoordinates); // save tracking logs
this.providerRouteCoordinates = []; // clear the old routes coordinates
}
}, (error) => {
console.log(error)
}, {
timeout: WATCH_LOCATION_TIMEOUT,
distanceFilter: 5,
enableHighAccuracy: true,
maximumAge: 0
});
}
/**
* Method To Pause Tracking
*/
pauseTracking() {
navigator.geolocation.getCurrentPosition((response) => {
if (response && response.coords) {
this.saveProviderPauseLogs(response.coords); // api method to save pause logs
}
},
(error) => {
console.log(error)
},
GeolocationOptions
);
}
/**
* Method To Resume Tracking
*/
resumeTracking() {
navigator.geolocation.getCurrentPosition((response) => {
if (response && response.coords) {
this.saveProviderResumeLogs(response.coords); // api method to save resume logs
}
},
(error) => {
console.log(error)
},
GeolocationOptions
);
}
/**
* Method To Stop Location Tracking
*/
stopTracking() {
this.isRunningService = false;
navigator.geolocation.clearWatch(this.watchId);
}
/**
* Method To Calculate Booking Location Distance From Provider Location
*/
calculateBookingLocationDistanceFromProviderLocation(lat1, lon1, lat2, lon2) {
var R = 6371; // km (change this constant to get miles)
var dLat = (lat2 - lat1) * Math.PI / 180;
var dLon = (lon2 - lon1) * Math.PI / 180;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return Math.round(d * 1000);
}
}
export default LocationTrackingService;
任何帮助将不胜感激,谢谢!
注意:我的清单中有