如何将数据写入 Firebase 实时数据库?
How to write data into Firebase Realtime Database?
我正在尝试以 2000 毫秒的间隔将用户位置数据写入我们的 Firebase RDB。
控制台显示我们每 2 秒就能正确获取位置数据。但是,数据不会传输到我们的 Firebase RDB 中。
有人知道如何解决这个问题吗?
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));
}
使用库 AngularFire
比使用 HttpClient
和执行 http 请求更容易和更有效。
对于rxample,可以使用push()
方法添加数据:
const itemsRef = db.list('items');
itemsRef.push({ name: newName });
https://github.com/angular/angularfire/blob/master/README.md#install
我刚刚找到了解决方案。请在下面找到代码。
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
});
我正在尝试以 2000 毫秒的间隔将用户位置数据写入我们的 Firebase RDB。 控制台显示我们每 2 秒就能正确获取位置数据。但是,数据不会传输到我们的 Firebase RDB 中。
有人知道如何解决这个问题吗?
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));
}
使用库 AngularFire
比使用 HttpClient
和执行 http 请求更容易和更有效。
对于rxample,可以使用push()
方法添加数据:
const itemsRef = db.list('items');
itemsRef.push({ name: newName });
https://github.com/angular/angularfire/blob/master/README.md#install
我刚刚找到了解决方案。请在下面找到代码。
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
});