本文介绍了记录坐标,在几秒钟内更新.在离子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,它记录位置,但是更新时间为5秒,我想将其减少到500毫秒左右.该应用程序正常运行,只是希望减少时间

Here is my code which logs the location but the update time is 5sec and I want to reduce it to around 500ms. the app is working properly just want to reduce the time

  import { Component, NgZone } from '@angular/core';
import { Plugins } from '@capacitor/core';
const { Geolocation } = Plugins;
import { NativeGeocoder, NativeGeocoderResult, NativeGeocoderOptions } from '@ionic-native/native-geocoder/ngx';
@Component({
  selector: 'app-tab3',
  templateUrl: 'tab3.page.html',
  styleUrls: ['tab3.page.scss']
})
export class Tab3Page {
  public lat: any;
  public lng: any;
  points: any;
  wait: any;
  constructor(public ngZone: NgZone) {
    this.points = [];
  }
  track() {
    this.wait = Geolocation.watchPosition({}, (position, err) => {
      this.ngZone.run(() => {
        this.lat = position.coords.latitude;
        this.lng = position.coords.longitude;
        let date = new Date().toLocaleTimeString();
        this.addNewLocation(position.coords.latitude, position.coords.longitude,date);
      })
    })
  }
  stopTracking() {
    Geolocation.clearWatch({ id: this.wait });
  }
  addNewLocation(lat, lng, time) {
    this.points.unshift({
      lat,
      lng,
      time,
    });
  }
}

推荐答案

请尝试利用超时选项:

您可以像这样使用它:

track() {
    this.wait = Geolocation.watchPosition({ enableHighAccuracy: false, timeout: 500, maximumAge: 0 }, (position, err) => {
      this.ngZone.run(() => {
        this.lat = position.coords.latitude;
        this.lng = position.coords.longitude;
        let date = new Date().toLocaleTimeString();
        this.addNewLocation(position.coords.latitude, position.coords.longitude,date);
      })
    })
  }

电容器文档也确认此方法可以正常工作: https://capacitorjs.com/docs /apis/geolocation#type-162408

Capacitor docs also confirm this should work: https://capacitorjs.com/docs/apis/geolocation#type-162408

如果watchPosition方法没有帮助您的用例,则可以尝试实现自己的"watch"工具.方法.不幸的是,我无法使用带有电容器的应用程序,但是如下所示的内容应该可以使您走上正轨:

If the watchPosition method is not helping your use case you could try implementing your own "watch" method. Unfortunately I do not have access to an app with capacitor, but something like the below should get you on the right track:

import { Component, NgZone } from '@angular/core';
import { Plugins } from '@capacitor/core';
const { Geolocation } = Plugins;
import { NativeGeocoder, NativeGeocoderResult, NativeGeocoderOptions } from '@ionic-native/native-geocoder/ngx';
import { interval } from 'rxjs';
import { Subscription } from 'rxjs';
import { concatMap } from 'rxjs/operators';

@Component({
  selector: 'app-tab3',
  templateUrl: 'tab3.page.html',
  styleUrls: ['tab3.page.scss']
})
export class Tab3Page {

  private subscription: Subscription;

  constructor(public ngZone: NgZone) {
  }

  track() {
    const interval500 = interval(500);
    this.subscription = interval500.pipe(
        concatMap(val => Geolocation.getCurrentPosition())
    ).subscribe(coordinates => console.log(coordinates));
  }

  stopTracking() {
    this.subscription.unsubscribe();
  }

}

基本上,您将需要自己的时间间隔(500ms),然后为每个"tick"调用getCurrentPosition方法.时间间隔.然后适当退订.

Basically you ll need your own interval (500ms) and then calling getCurrentPosition method for each "tick" of the interval. Then unsubscribing duly.

实际上不建议这样做,因为这会导致电池问题等.此外,如果设备在更新数量上受到设备级别的限制,则该设备可能仍无法提供地理数据.

This is really not recommended way as this will cause battery issues etc.Also the device might not be able to supply the geo data still if it has device level limitation on amount of updates.

根据使用情况,您可能要近似"使用.而是每隔5秒间隔添加一次地理数据.

Depending on the use case you might want to "approximate" geo data in between 5 second intervals instead.

这篇关于记录坐标,在几秒钟内更新.在离子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:58