Professional Documents
Culture Documents
https://dev.to/raymondcamden/getting-location-in-nativescript-1jkn 1/5
14/4/2019 Getting Location in NativeScript - DEV Community
Ok, so that’s easy. Using the plugin is mostly easy, but you do
have to handle permissions as well as handling errors from
retrieving the data. In my opinion, the main readme doesn’t
do a great job of showing this in a complete example (although
more on that in a bit), so I had to guess a bit to figure it out,
but here’s what I came up with as a general “flow”:
Geolocation.enableLocationRequest(true)
.then(() => {
Geolocation.isEnabled().then(isLocationEnabled => {
if(!isLocationEnabled) {
// potentially do more then just end here...
return;
}
is never made. That seems like a small bug to me, but it’s easy
enough to work around.
<template>
<Page class="page">
<ActionBar class="action-bar">
<Label class="action-bar-title" text="Geolocation Demo"></Label>
</ActionBar>
<StackLayout>
<Label v-if="needLocation" text="Looking up your location..." />
<Label v-if="locationFailure" text="Sorry, I failed! :(" />
<Label v-if="location" :text="locationDescription" textWrap="true" />
</StackLayout>
</Page>
</template>
<script>
import * as Geolocation from 'nativescript-geolocation';
export default {
data() {
return {
needLocation:true,
locationFailure:false,
location:null
}
},
computed: {
locationDescription() {
2 2 2
https://dev.to/raymondcamden/getting-location-in-nativescript-1jkn 3/5
14/4/2019 Getting Location in NativeScript - DEV Community
Geolocation.enableLocationRequest(true)
.then(() => {
Geolocation.isEnabled().then(isLocationEnabled => {
console.log('result is '+isLocationEnabled);
if(!isLocationEnabled) {
this.needLocation = false;
this.locationFailure = true;
// potentially do more then just end here...
return;
}
And that’s it. In my next post I’m going to dig a bit deeper. The
plugin has some useful methods you may be familiar with
already from the web API, but it also has more including a
super useful distance API built in. As always, let me know if
you have any questions by leaving a comment below!
2 2 2
https://dev.to/raymondcamden/getting-location-in-nativescript-1jkn 5/5