Created
June 27, 2021 15:37
-
-
Save cristofima/c5913575dd5fe4fada425cbfc1efedbd to your computer and use it in GitHub Desktop.
Service to get coordinates from mobile GPS using Ionic framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx'; | |
import { LocationAccuracy } from '@ionic-native/location-accuracy/ngx'; | |
import { Geolocation, GeolocationOptions } from '@ionic-native/geolocation/ngx'; | |
import { Platform } from '@ionic/angular'; | |
export interface ILatLng { | |
latitude: number; | |
longitude: number; | |
speed?: number; | |
altitude?: number; | |
} | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class GpsService { | |
constructor(private platform: Platform, | |
private geolocation: Geolocation, | |
private locationAccuracy: LocationAccuracy, | |
private androidPermissions: AndroidPermissions) { } | |
getPosition(): Promise<ILatLng>{ | |
return new Promise((resolve, reject) => { | |
if(this.platform.is("mobileweb")){ | |
this.getCurrentPosition().then(r =>{ | |
resolve(r); | |
}).catch(()=>{ | |
reject(); | |
}); | |
}else{ | |
const isAndroid = this.platform.is("android"); | |
if(isAndroid){ | |
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION).then( | |
result => { | |
if (result.hasPermission) { | |
//If having permission show 'Turn On GPS' dialogue | |
this.askToTurnOnGPS().then(r =>{ | |
resolve(r); | |
}).catch(()=>{ | |
reject(); | |
}); | |
} else { | |
//If not having permission ask for permission | |
this.requestGPSPermission().then(r =>{ | |
resolve(r); | |
}).catch(()=>{ | |
reject(); | |
}); | |
} | |
}); | |
}else{ | |
this.askToTurnOnGPS().then(r =>{ | |
resolve(r); | |
}).catch(()=>{ | |
reject(); | |
}); | |
} | |
} | |
}); | |
} | |
private getCurrentPosition(): Promise<ILatLng>{ | |
return new Promise((resolve, reject) => { | |
const options: GeolocationOptions = { | |
maximumAge: 10000, | |
timeout: 20000 | |
}; | |
this.geolocation.getCurrentPosition(options).then((resp) => { | |
if(resp?.coords){ | |
let latLng: ILatLng = { | |
latitude: resp.coords.latitude, | |
longitude: resp.coords.longitude, | |
altitude: resp.coords.altitude | |
}; | |
resolve(latLng); | |
}else{ | |
reject('Empty coordinates'); | |
} | |
}).catch((error) => { | |
reject('Error getting location: '+ error); | |
}); | |
}); | |
} | |
private askToTurnOnGPS(): Promise<ILatLng>{ | |
return new Promise((resolve, reject) => { | |
this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then( | |
() => { | |
setTimeout(()=>{ | |
this.getCurrentPosition().then(r =>{ | |
resolve(r); | |
}).catch(()=>{ | |
reject(); | |
}); | |
}, 1000); | |
} | |
).catch(()=>{ | |
reject(); | |
}); | |
}); | |
} | |
private requestGPSPermission(): Promise<ILatLng>{ | |
return new Promise((resolve, reject) =>{ | |
this.locationAccuracy.canRequest().then((canRequest: boolean) => { | |
if (canRequest) { | |
this.askToTurnOnGPS().then(r =>{ | |
resolve(r); | |
}).catch(()=>{ | |
reject(); | |
}); | |
}else{ | |
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION) | |
.then( | |
() => { | |
this.askToTurnOnGPS().then(r =>{ | |
resolve(r); | |
}).catch(()=>{ | |
reject(); | |
}); | |
}).catch(()=>{ | |
reject(); | |
}); | |
} | |
}).catch(()=>{ | |
reject(); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment