Rafael Moura
Rafael Moura

Reputation: 1327

Watch Position Ionic 3

Hi guys I need help in how to watch position in google maps with Ionic 3, I would like that upload position in real time in the firebase In my home.ts I have the following code :

    getMyPosition() {
    this.geolocation.getCurrentPosition().then((result) => {
      this.positionTruck = new google.maps.LatLng(result.coords.latitude, result.coords.longitude);
      const mapOptions = {
        zoom: 18,
        center: this.positionTruck,
        disableDefaultUI: true
      }    
      this.map = new google.maps.Map(document.getElementById('map'), mapOptions);
      this.truckMarker(this.positionTruck);

      let watch = this.geolocation.watchPosition();
      watch.subscribe((data) => {
      let truck = { latitude: data.coords.latitude, longitude: data.coords.longitude };
        this.truckService.updateGeolocation(this.truck.key, truck);
        console.log(data.coords)
      });

    }).catch((error) => {
      console.log('Erro ao tentar pegar sua localização ', error);
    })
  }

And now I have this code in service to update truck.service:

    update(key: string, truck: any) {
    return new Promise((resolve, reject) => {
      this.db.list(this.PATH)
            .update(key, (truck))
            .then(() => resolve())
            .catch((e) => reject())
    })
  }

this way when I test on the device, the home page giving refresh in page, why ? Is this form correctly the update position ? help please.

Upvotes: 0

Views: 1900

Answers (1)

Grenoblois
Grenoblois

Reputation: 559

import : import { Geolocation, Geoposition } from '@ionic-native/geolocation';

and declare :

public lat: number = 0;
public lng: number = 0; 

and add in your code

let options = {
      frequency: 3000,
      enableHighAccuracy: true
    };

this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {

  console.log(position.coords.latitude + '   ++++++++++ ' + position.coords.longitude);

  // Run update inside of Angular's zone
  this.zone.run(() => {
    this.lat = position.coords.latitude;
    this.lng = position.coords.longitude;
    );

  });

});

Upvotes: 1

Related Questions