save latitude and longitude by distance (Google Maps and Ionic 3)

0

I have the following function to save save latitude and longitude in firebase, but I see that it saves it very fast, that is, approximately in 20 minutes I keep around 400 positions.

    //empieza geololaizacion
start() {
    let markes = [];
   // Compruebo si esta habilidata la opcion de localizacion
  this.backgroundGeolocation.isLocationEnabled()
  .then((activado) =>{
      //si esta activado
    if(activado){

      let config = {
        desiredAccuracy: 0,
        stationaryRadius: 0,
        distanceFilter: 0,
        debug: false,
        interval: 1000
      };
      //Geolocalizacion en segundo Plano      
      this.backgroundGeolocation
      .configure(config)
      .subscribe((location) => {
        console.log("latitud actualizacion background 1"+location.latitude);

        this.zone.run(() => {
        this.lat = location.latitude;
        this.lng = location.longitude;
        });
        this.actuario.update({
            lat: location.latitude,
            lng: location.longitude
        });
           markes.push({
             latitud:location.latitude,
             longitud:location.longitude
           });
           this.actuario.set({markes}, { merge: true })
                   .then(function() {})
                   .catch(function(error) {
                   console.log("Error al subir datos! " + error);
             });


        });

      // empieza actualizacion si se sale de la aplicacion
      this.backgroundGeolocation.start();
      let options = {
        frequency: 3000,
        enableHighAccuracy: true
    };
        //cuando la aplicacion esta abierta y activada
      this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined)
      .subscribe((position: Geoposition) => {
        console.log("latitud actualizacion 2"+position.coords.latitude + "  " + position.coords.longitude);
       //actualizo dato en firebase
        this.actuario.update({
              lat: position.coords.latitude,
              lng: position.coords.longitude
          });
          //para poder mapear en un mapa se guardan los datos
             markes.push({
               latitud:position.coords.latitude,
               longitud:position.coords.longitude
             });
             this.actuario.set({markes}, { merge: true })
                     .then(function() {})
                     .catch(function(error) {
                     console.log("Error al subir datos! " + error);
               });
      });

    }else {
      this.backgroundGeolocation.showLocationSettings();
    }
  }) 
}

The first option is to save those latitudes and longitudes by 3 meters, or save the data every 5 minutes, Is there any configuration with google maps to do this?,

    
asked by Eze 31.07.2018 в 00:26
source

1 answer

1

According to the documentation that appears in the repository , the settings that you are going through:

 let config = {
    desiredAccuracy: 0,
    stationaryRadius: 0,
    distanceFilter: 0,
    debug: false,
    interval: 1000
  };

It has two effects. First: interval: 1000 means updating every one second. According to the documentation:

  

interval: (Android only) The minimum time interval between location updates in milliseconds. @see Android docs for more   information.

If you saved 400 positions in 20 minutes it means that you are updating every 3 seconds. This is because the geolocation of the device or browser can not respond faster than that.

If you were to change interval: 300000 you would be updating every 5 minutes.

The second effect is that you set stationaryRadius: 0 , that is, even if the person has not moved from their previous location, you trigger the event of storing the same position. According to the documentation:

  

stationaryRadius: Stationary radius in meters. When stopped, the minimum distance of the device must move beyond the stationary location   for aggressive background-tracking to engage.

If you were to change stationaryRadius: 10 you would only trigger the event if the person has moved at least 10 meters since the last query.

Apparently the latter requires an additional setting in iOS (to force the use of FOREGROUND mode), so if you do not find it you could also try using distanceFilter . You have this parameter at zero, which, again, implies that you are always triggering the request that stores the position. If you put distanceFilter:10 instead, you would only do it if the person has moved at least 10 meters. PEEERO the documentation says:

  

distanceFilter : The minimum distance (measured in meters) to device must move horizontally before an update event is generated.

It sounds weird that you only detect the position in the X coordinate, but try it anyway.

Summary:

Play with the parameters increasing stationaryRadius , distanceFilter e interval until you get the desired frequency. I would start with stationaryRadius that seems to work on both iOS and Android and sounds like a pretty sensible restriction.

PS: I have never used this library and I have not used Ionic for 5 years. I just read the documentation.

    
answered by 31.07.2018 / 02:11
source