ionic2
Geolocation
Sök…
Enkel användning
package.json
till att inkludera beroenden i ditt package.json
:
{
...
"dependencies": {
...
"ionic-native": "^1.3.10",
...
},
...
}
Så här använder du geolocation:
// custom-component.ts
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 a promise
Geolocation.getCurrentPosition().then((position:Geoposition)=> {
console.log(
position.coords.latitude,
position.coords.longitude);
});
}
}
Titta på positionen
För en mer realtidslösning kan du använda watchPosition-funktionen i Geolocation som meddelar när ett fel eller en positionsändring inträffar. Till skillnad från getCurrentPosition returnerar watchPosition en observerbar
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);
});
}
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow