【发布时间】:2020-06-14 04:16:51
【问题描述】:
我正在尝试构建一个 Ionic (Angular) 地理定位跟踪器移动应用程序。位置跟踪已经很好用了。但现在我想将地理位置存储在路线中。目前,每个数据集(准确度、经度、纬度...)都将单独保存在 Firebase 实时数据库中。
有人知道怎么解决吗?
目标是有这样的结构:
-geolocations
- routeId
- trackingId
- accuracy
- long
- lat
- timeStamp
- uId
tracking.page.ts
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation/ngx';
import { GeolocationService } from '../../app/geolocation.service';
import { UserService } from '../../app/user.service';
import { Insomnia } from '@ionic-native/insomnia/ngx';
import { LoadingController } from '@ionic/angular';
import { AlertController } from '@ionic/angular';
import { AppComponent } from '../app.component';
import { Storage } from '@ionic/storage';
declare var google;
@Component({
selector: 'app-tracking',
templateUrl: './tracking.page.html',
styleUrls: ['./tracking.page.scss'],
})
export class TrackingPage implements OnInit {
@ViewChild('map', { static: true }) mapElement: ElementRef;
map: any;
markers = [];
geoLocations: any;
watchLocationUpdates: any;
isWatching: boolean;
interval: any;
geoLatitude: number;
geoLongitude: number;
geoAccuracy: number;
timeStamp: any;
uId: string;
trackingId: string;
constructor(
private geolocation: Geolocation,
public geolocationService: GeolocationService,
public userService: UserService,
private insomnia: Insomnia,
public loadingCtrl: LoadingController,
public alertController: AlertController,
public appComponent: AppComponent,
private storage: Storage
) {
}
ngOnInit() {
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
console.log('navigator.geolocation works well');
}
this.appComponent.isLoggedIn = true;
this.loadMap();
console.log('this.loadMap() called');
}
loadMap() {
const latLng = new google.maps.LatLng(48.141558, 11.568210);
const mapOptions = {
center: latLng,
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
streetViewControl: false,
fullscreenControl: false,
zoomControl: false
};
this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
}
// Start location watch
watchLocation() {
this.loadingCtrl.create({
message: 'Standort Tracking wird initialisiert...',
duration: 5000,
}).then((res) => {
res.present();
});
const options = {
maximumAge: 3000,
timeout: 5000,
enableHighAccuracy: true,
};
this.isWatching = true;
this.insomnia.keepAwake()
.then(
() => console.log('this.insomnia.keepAwake(): success')
);
this.trackingId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
this.interval = setInterval(() => {
this.uId = this.userService.uId;
if (this.appComponent.userName !== '') { // Makes sure that username is loaded from db
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 = Date.now();
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
});
});
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: '#1CA0EC',
fillOpacity: 1,
strokeColor: 'white',
strokeWeight: 2
},
position: latLng
});
this.markers.push(marker);
});
} else {
this.stopLocationWatch();
}
}, 3000);
}
// Stop location watch
stopLocationWatch() {
this.isWatching = false;
console.log('this.isWatching = ', this.isWatching);
clearInterval(this.interval);
console.log('this.interval', this.interval);
// this.watchLocationUpdatesSub.unsubscribe();
this.loadMap();
this.locationStopAlert();
this.insomnia.allowSleepAgain()
.then(
() => console.log('this.insomnia.allowSleepAgain(): success'),
);
}
async locationStopAlert() {
const alert = await this.alertController.create({
header: 'Das Standort Tracking wurde beendet',
// message: 'This is an alert message.',
buttons: ['OK']
});
await alert.present();
}
}
【问题讨论】:
-
你能添加一个旧的json结构的例子吗?trackingId在路由中是唯一的吗?
-
是的 trackingId 应该是唯一的。这样每条路线都可以有多个数据集(准确度、经度、纬度等)。我可能还必须更改数据服务以首先将 routeId 存储在数据库中,然后将 trackingIds 与相关数据集一起存储。但我不知道如何将页面的属性(例如 tracking.page)导入数据服务。
标签: json angular typescript ionic-framework