import { Component, OnInit } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation/ngx';
import { HttpClient } from '@angular/common/http';
import { BackgroundMode } from '@ionic-native/background-mode/ngx';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private geolocation: Geolocation, private http: HttpClient, private backgroundMode: BackgroundMode) {}
ngOnInit() {
this.backgroundMode.enable();
this.getGeolocation();
}
getGeolocation() {
this.geolocation.getCurrentPosition().then((resp) => {
this.sendPosition(resp.coords.latitude, resp.coords.longitude);
setInterval(() => {
this.geolocation.getCurrentPosition().then((resp) => {
this.sendPosition(resp.coords.latitude, resp.coords.longitude);
});
}, 30000);
});
}
sendPosition(latitude: number, longitude: number) {
this.http.post('https://your-url.com/post-position', { latitude, longitude }).subscribe();
}
}
Este código utiliza la biblioteca de geolocalización de Ionic Native para obtener la posición GPS y la biblioteca de modo de fondo de Ionic Native para ejecutar la aplicación en segundo plano. La posición GPS se envía a una URL específica cada 30 segundos utilizando el método HTTPClient.post. Recuerda reemplazar “https://your-url.com/post-position” con la URL que deseas usar.