Currently, I am facing an issue where user location data is not being written into our Firebase Realtime Database even though the console confirms that the data is fetched correctly every 2 seconds.
I am seeking assistance on how to resolve this problem.
tracking.page.ts
// Start location watch
watchLocation() {
const options = {
maximumAge: 15000,
timeout: 5000,
enableHighAccuracy: true,
};
this.isWatching = true;
this.trackingId = '-' + Math.random().toString(36).substr(2, 28);
clearInterval(this.interval);
this.interval = setInterval(() => {
this.watchLocationUpdates = this.geolocation.getCurrentPosition(options);
this.watchLocationUpdates.then((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;
this.geolocationService.insertUserGeolocation({
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
});
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);
});
}, 2000);
}
geolocation.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../environments/environment';
@Injectable({
providedIn: 'root'
})
export class GeolocationService {
databaseUrl = environment.firebase.databaseURL;
constructor(private http: HttpClient) {
console.log('Hello OrganizationService Provider');
console.log('OrganizationService: ', this.databaseUrl);
}
insertUserGeolocation(data) {
return this.http.post(`${this.databaseUrl}/geolocations/.json`, data);
console.log('insertUserGeolocation(data): this.insertUserGeolocation(data));
}