I'm currently working on developing an iOS location tracking application using the Ionic 5 Framework, Angular, and the Cordova Geolocation Plugin.
In the past, I was able to track user location changes using the watchPosition() function, which worked well on Android devices. However, I encountered issues with this functionality not working on iOS devices.
My new approach involves using the getCurrentPosition() function in conjunction with setInterval() to retrieve the current location and update it every one or two seconds.
Unfortunately, I'm now encountering the following error:
TypeError: this.watchLocationUpdates.subscribe is not a function
Does anyone have any suggestions on how to resolve this issue?
watchLocation() {
const options = {
maximumAge: 3600000,
timeout: 5000,
enableHighAccuracy: true,
};
this.isWatching = true;
this.trackingId = '-' + Math.random().toString(36).substr(2, 28);
// Please find relevant setInterval() part below
this.watchLocationUpdates = setInterval(() => { this.geolocation.getCurrentPosition(options); }, 1000);
this.watchLocationUpdatesSub = this.watchLocationUpdates.subscribe((resp) => {
this.geoLocations = resp.coords;
this.geoLatitude = resp.coords.latitude;
this.geoLongitude = resp.coords.longitude;
this.geoAccuracy = Math.trunc(resp.coords.accuracy);
this.timeStamp = resp.timestamp;
const position = new google.maps.LatLng(resp.coords.latitude, resp.coords.longitude);
this.map.setCenter(position);
this.map.setZoom(16);
this.markers.map(marker => marker.setMap(null));
this.markers = [];
const latLng = new google.maps.LatLng(resp.coords.latitude, resp.coords.longitude);
const marker = new google.maps.Marker({
map: this.map,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 13,
fillColor: '#1AA0EC',
fillOpacity: 1,
strokeColor: 'white',
strokeWeight: 2
},
position: latLng
});
this.markers.push(marker);
console.table('watchLocation function called', {
trackingId: this.trackingId,
latitude: this.geoLatitude,
longitude: this.geoLongitude,
accuracy: this.geoAccuracy,
timeStamp: this.timeStamp,
uId: this.uId
});
this.geolocationService.insertUserGeolocation({
trackingId: this.trackingId,
latitude: this.geoLatitude,
longitude: this.geoLongitude,
accuracy: this.geoAccuracy,
timeStamp: this.timeStamp,
uId: this.uId
})
.subscribe((response) => {
localStorage.setItem('lastLocation', JSON.stringify({
trackingId: this.trackingId,
latitude: this.geoLatitude,
longitude: this.geoLongitude,
accuracy: this.geoAccuracy,
timeStamp: this.timeStamp,
uId: this.uId
}));
console.log(`user location data inserted in FB`, {
trackingId: this.trackingId,
latitude: this.geoLatitude,
longitude: this.geoLongitude,
accuracy: this.geoAccuracy,
timeStamp: this.timeStamp,
uId: this.uId
});
});
});
}