You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2018/11/01 09:56:12 UTC
[GitHub] terminaltor commented on issue #114: Not working on Android devices
terminaltor commented on issue #114: Not working on Android devices
URL: https://github.com/apache/cordova-plugin-geolocation/issues/114#issuecomment-434990477
Hello everyone...
After few weeks fighting this non-working Android situation, here is a full example working in both platforms.
Ionic 3, Angular 5.
```
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Platform} from 'ionic-angular';
import {Geolocation, Geoposition} from '@ionic-native/geolocation';
import {LocationAccuracy} from "@ionic-native/location-accuracy";
import {Diagnostic} from "@ionic-native/diagnostic";
@Injectable()
export class GlobalDataService {
constructor(private http: HttpClient,
public platform: Platform,
public geo: Geolocation,
public diagnostic: Diagnostic,
public locationAccuracy: LocationAccuracy
) {}
getUserPosition() {
return new Promise(resolve => {
const HIGH_ACCURACY = 'high_accuracy';
if (this.platform.is('cordova')) {
this.platform.ready().then(() => {
this.diagnostic.isLocationEnabled().then(enabled => {
if (enabled) {
if (this.platform.is('android')) {
this.diagnostic.getLocationMode().then(locationMode => {
if (locationMode === HIGH_ACCURACY) {
this.geo.getCurrentPosition({timeout: 30000, maximumAge: 0, enableHighAccuracy: true}).then(pos => {
resolve({
coords: {
latitude: pos.coords.latitude,
longitude: pos.coords.longitude
}
});
}).catch(error => resolve(error));
} else {
this.askForHighAccuracy().then(available => {
if (available) {
this.getUserPosition().then(a => resolve(a), e => resolve(e));
}
}, error => resolve(error));
}
});
} else {
this.geo.getCurrentPosition({timeout: 30000, maximumAge: 0, enableHighAccuracy: true}).then(pos => {
resolve({
coords: {
latitude: pos.coords.latitude,
longitude: pos.coords.longitude
}
});
}).catch(error => resolve(error));
}
} else {
this.locationAccuracy.request(1).then(result => {
if (result) {
this.getUserPosition().then(result => resolve(result), error => resolve(error));
}
}, error => {
resolve(error)
});
}
}, error => {
resolve(error)
});
});
} else {
resolve('Cordova is not available');
}
});
}
askForHighAccuracy(): Promise<Geoposition> {
return new Promise(resolve => {
this.locationAccuracy
.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(() => {
this.geo.getCurrentPosition({timeout: 30000}).then(
position => {
resolve(position);
}, error => resolve(error)
);
}, error => resolve(error));
});
}
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org