Skip to content

Commit

Permalink
Stop watching position when the map is not visible
Browse files Browse the repository at this point in the history
Closes #25
  • Loading branch information
salah3x committed Jul 29, 2019
1 parent 36f8168 commit c17bc10
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/app/map/map.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export class MapPage implements OnInit {
lng: -7.5984837
};
center: { lat: number; lng: number };
positionWatcher: string;
user$: Observable<User>;
friends$: Observable<User[]>;
centerToMyPosition = true;

constructor(private store: StoreService, private route: ActivatedRoute) {}

Expand All @@ -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 });
}
}

0 comments on commit c17bc10

Please sign in to comment.