From c17bc103a77aa77585953bd01c46826dc72f7272 Mon Sep 17 00:00:00 2001 From: salah3x Date: Tue, 30 Jul 2019 00:55:35 +0100 Subject: [PATCH] Stop watching position when the map is not visible Closes #25 --- src/app/map/map.page.ts | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/app/map/map.page.ts b/src/app/map/map.page.ts index c045499..5eff8ca 100644 --- a/src/app/map/map.page.ts +++ b/src/app/map/map.page.ts @@ -17,8 +17,10 @@ export class MapPage implements OnInit { lng: -7.5984837 }; center: { lat: number; lng: number }; + positionWatcher: string; user$: Observable; friends$: Observable; + centerToMyPosition = true; constructor(private store: StoreService, private route: ActivatedRoute) {} @@ -31,18 +33,27 @@ export class MapPage implements OnInit { lng: +params.get('longitude') || this.myPosition.lng }; }); - let centerToMyPosition = true; - Plugins.Geolocation.watchPosition({}, (position: Position) => { - if (position) { - this.myPosition = { - lat: position.coords.latitude, - lng: position.coords.longitude - }; - if (centerToMyPosition) { - this.center = { ...this.myPosition }; - centerToMyPosition = false; + } + + ionViewWillEnter() { + this.positionWatcher = Plugins.Geolocation.watchPosition( + {}, + (position: Position) => { + if (position) { + this.myPosition = { + lat: position.coords.latitude, + lng: position.coords.longitude + }; + if (this.centerToMyPosition) { + this.center = { ...this.myPosition }; + this.centerToMyPosition = false; + } } } - }); + ); + } + + ionViewDidLeave() { + Plugins.Geolocation.clearWatch({ id: this.positionWatcher }); } }