The Geolocation API provides simple methods for getting and tracking the current position of the device using GPS, along with altitude, heading, and speed information if available.
Geolocation.getCurrentPosition({
enableHighAccuracy: true
})
.then(function(result) {
console.log('Coords:', result.coords);
console.log('Timestamp:', result.timestamp);
})
.catch(function(e) {
console.error('Unable to get current position:', e);
});
...
var watch = Geolocation.watchPosition();
var subscription = watch.subscribe((data) => {
console.log(data);
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
});
...
// Later:
// This cancels the ongoing Observable execution which
// was started by calling subscribe with an Observer.
subscription.unsubscribe();
window.Geolocation.getCurrentPosition({
enableHighAccuracy: true,
})
.then(function (result) {
console.log("Coords:", result.coords);
console.log("Timestamp:", result.timestamp);
})
.catch(function (e) {
console.error("Unable to get current position:", e);
});
...
var watch = window.Geolocation.watchPosition();
setwatchPositionSubscrition(watch.subscribe((data) => {
console.log(data);
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
}));
...
setwatchPositionSubscrition(watchPositionSubscrition.unsubscribe());
window.Geolocation.getCurrentPosition({
enableHighAccuracy: true,
})
.then(function (result) {
console.log("Coords:", result.coords);
console.log("Timestamp:", result.timestamp);
})
.catch(function (e) {
console.error("Unable to get current position:", e);
});
...
this.watch = window.Geolocation.watchPosition();
this.subscription = this.watch.subscribe((data) => {
console.log(data);
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
});
...
this.subscription.unsubscribe();
(<any>window).Geolocation.getCurrentPosition({
enableHighAccuracy: true,
})
.then(function (result) {
console.log("Coords:", result.coords);
console.log("Timestamp:", result.timestamp);
})
.catch(function (e) {
console.error("Unable to get current position:", e);
});
...
this.watch = (<any>window).Geolocation.watchPosition();
this.subscription = this.watch.subscribe((data) => {
console.log(data);
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
});
...
this.subscription.unsubscribe();
window.Geolocation.getCurrentPosition({
enableHighAccuracy: true,
})
.then(function (result) {
console.log("Coords:", result.coords);
console.log("Timestamp:", result.timestamp);
})
.catch(function (e) {
console.error("Unable to get current position:", e);
});
...
watch = window.Geolocation.watchPosition();
subscription = watch.subscribe((data) => {
console.log(data);
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
});
...
subscription.unsubscribe();
getCurrentPosition(options: GeolocationOptions): Promise <GeolocationPosition>
Get the device's current position.
watchPosition(options?: GeolocationOptions): Observable <GeolocationPosition>
Watch the current device's position. Clear the watch by unsubscribing from Observable changes..
interface GeolocationOptions {
enableHighAccuracy ?: boolean;
maximumAge ?: number;
timeout ?: number;
}
interface GeolocationPosition {
// The GPS coordinates along with the accuracy of the data
coords : Coordinates;
// Creation timestamp for coords
timestamp : int;
}
interface Coordinates {
// Accuracy of the latitude and longitude properties, expressed in meters.
accuracy : int,
// Position's altitude in metres, relative to sea level.
altitude : int,
// Accuracy of the altitude expressed in meters. This value can be null.
altitudeAccuracy : int,
// Direction in which the device is traveling.
heading : int,
// Position's latitude in decimal degrees.
latitude : int,
// Position's longitude in decimal degrees.
longitude : int,
// Velocity of the device in meters per second. This value can be null.
speed : int
}