2017-11-20 46 views
10

Plugin vị trí địa lý nền cho ionic không cập nhật. Các chức năng tôi muốn là mỗi 30 giây yêu cầu các plugin cho một giá trị lng lat nếu có. Vấn đề là, nó chỉ cho tôi những giá trị ban đầu và sau đó nền dừng lại. Nền trước là tốt, nó thực sự là nền tảng. Về cơ bản, tôi không thể gửi yêu cầu sau lần gửi ban đầu đầu tiên trong nền.vị trí địa lý nền ionic 3 không cập nhật

gps.ts

startTracking() { 

    console.log('started tracking') 
    const config: BackgroundGeolocationConfig = { 
     desiredAccuracy: 10, 
     stationaryRadius: 20, 
     distanceFilter: 30, 
     debug: false, // enable this hear sounds for background-geolocation life-cycle. 
     stopOnTerminate: false 
    }; 

    this.backgroundGeolocation.configure(config) 
    .subscribe((location: BackgroundGeolocationResponse) => { 


     this.zone.run(() => { 
     this.lat = location.latitude 
     this.lng = location.longitude 
     this.bearing = location.bearing 
     this.speed = location.speed 
     this.accuracy = location.accuracy 
     this.timestamp = location.time 
     }) 


     this.backgroundGeolocation.finish(); // FOR IOS ONLY 
     this.backgroundGeolocation.stop() 

     }); 


    this.backgroundGeolocation.start(); 

    } 

sendGPS(){ 
this.optionsService.sendGPS(gpsData).subscribe(result => { 
      } 
     }) 
} 

stopTracking() { 

    this.sendGPS() 
} 

app.component.ts

constructor(){ 
this.sendGPSStart() 
this.interval() 
} 

sendGPSStart(){ 
    this.locationTracker.startTracking(); 
    } 

    sendGPSStop(){ 
    this.locationTracker.stopTracking(); 
} 

interval(){ 
setInterval(() => { 
     this.sendGPSStart() 
      this.sendGPSStop() 
    }, '30000') 

} 

Trả lời

1

Nhìn vào ví dụ ví dụ dnchia/Ionic3-Background-Geolocation bạn sẽ cấu hình khoảng thời gian trên nền, cũng như foreground định kỳ gửi

gps.ts

startTracking(interval) { 

    console.log('started tracking') 
    const config: BackgroundGeolocationConfig = { 
     desiredAccuracy: 10, 
     stationaryRadius: 20, 
     distanceFilter: 30, 
     debug: false, // enable this hear sounds for background-geolocation life-cycle. 
     stopOnTerminate: false, 
     interval: interval 
    }; 

app.component.ts

interval = 30000; 

constructor() { 
    this.sendGPSStart() 
    this.interval() 
} 

sendGPSStart(){ 
    this.locationTracker.startTracking(this.interval); 
} 

sendGPSStop(){ 
    this.locationTracker.stopTracking(); 
} 

interval() { 
    setInterval(() => { 
    this.locationTracker.sendGPS(); 
    }, this.interval) 

} 
+0

Thật không may, cách tiếp cận này chỉ hoạt động trong Android. – userlkjsflkdsvm

Các vấn đề liên quan