看著這個位置

對於更實時的解決方案,你可以在 Geolocation 中使用 watchPosition 函式,該函式會在發生錯誤或位置更改時通知。與 getCurrentPosition 不同,watchPosition 返回一個 Observable

import {Geolocation} from 'ionic-native';
import template from './custom-component.html';

@Component({
selector: 'custom-component',
template: template
})
export class CustomComponent {
constructor() {

    // get the geolocation through an observable
        Geolocation.watchPosition(<GeolocationOptions>{
            maximumAge: 5000, // a maximum age of cache is 5 seconds
            timeout: 10000, // time out after 10 seconds
            enableHighAccuracy: true // high accuracy 
        }).subscribe((position) => {
            console.log('Time:' + position.timestamp);
            console.log(
                'Position:' + position.coords.latitude + ',' +
                position.coords.longitude);
            console.log('Direction:' position.coords.heading);
            console.log('Speed:' position.coords.speed);

        });
}