ionic2 Geolocation Watching the position


Example

For a more real time solution you can use watchPosition function in Geolocation that notifies whenever an error or a position change occurs. Unlike the getCurrentPosition the watchPosition returns an 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);

        });
}