Поскольку я хочу использовать информацию о местоположении в своем приложении Ioni c 5 / Capacitor, я написал первую версию оболочки для Geolocation API .
Проблема в том, что, когда я внедряю эту услугу и звоню startReceivingLocationUpdates()
, я получаю следующую ошибку:
core.js:6014 ERROR TypeError: Cannot read property 'locationSubject' of undefined
at locationCallback (location.service.ts:51)
at window.navigator.geolocation.watchPosition.Object.enableHighAccuracy (geolocation.js:30)
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Object.onInvoke (core.js:39699)
at ZoneDelegate.invoke (zone-evergreen.js:358)
at Zone.runGuarded (zone-evergreen.js:134)
at zone-evergreen.js:118
Служба моего местоположения
import { Injectable } from '@angular/core';
import { Geolocation, GeolocationOptions, GeolocationPosition } from '@capacitor/core';
import { BehaviorSubject, Observable } from 'rxjs';
const locationOptions: GeolocationOptions = {
enableHighAccuracy: true,
requireAltitude: false
};
@Injectable({
providedIn: 'root'
})
export class LocationService {
private locationSubject = new BehaviorSubject<GeolocationPosition>(null);
private watchId: string = null;
constructor() {
}
receivingLocationUpdates(): boolean {
return this.watchId != null;
}
startReceivingLocationUpdates()
{
if(this.watchId !== null)
return;
this.watchId = Geolocation.watchPosition(locationOptions, this.locationCallback);
}
stopReceivingLocationUpdates()
{
if(this.watchId === null)
return;
Geolocation.clearWatch({id: this.watchId })
.then(_ => {
this.watchId = null;
});
}
subscribe(): Observable<GeolocationPosition>
{
return this.locationSubject.asObservable();
}
private locationCallback(location: GeolocationPosition) {
this.locationSubject.next(location);
}
}
Что я делаю неправильно?