Geolocation Ionic 3 Issue


#1

Hello all,

I’m having an issue with the Geolocation plugin.

When I subscribe to watchPosition function it returns an Observable.

After that , when I will leave the View , I want to unsubscribe from the Observable.

And I receive these message : this.watch.unsubscribe is not a function


#2

Hi@hiperium
Post your piece of code here.That may help you to get fix for the issue :smiley:


#3
public watch: Observable<Geoposition>;

this.watch.subscribe((data) => {
          let miPosicion = new LatLng(data.coords.latitude, data.coords.longitude);
          console.log("Entro en subscribe")
          if (this.myTarget != null) {
            this.myTarget.remove();
            this.map.addMarker({ icon: 'assets/imgs/customMarker.jpg', position: miPosicion }).then((marker) => { this.myTarget = marker });
          } else {
            this.map.addMarker({ icon: 'assets/imgs/customMarker.jpg', position: miPosicion }).then((marker) => { this.myTarget = marker });
          }
        })


ionViewWillLeave() {
    if (this.watch){
      console.log("Exists this.watch");
      this.watch.unsubscribe();
    }

So , when I leave the View , i want to unsubscribe from the observable. But it doesn’t recognize unsubscribe() like a function.

Thanks.


#4

@hiperium
Iam little bit confused :confused:
I think there is no unsubscribe funtion for geolocation.
you may be use geolocation.clearWatch function if you are using
ionic geolocation plugin native geolocation


#5

By the Ionic documentation , it has to be like these ->

const subscription = this.geolocation.watchPosition()
                              .filter((p) => p.coords !== undefined) //Filter Out Errors
                              .subscribe(position => {
  console.log(position.coords.longitude + ' ' + position.coords.latitude);
});

// To stop notifications
subscription.unsubscribe();

And that’s what I have…


#6

Hi@hiperium You had better the ionic official documentation for the reference