Geolocation.getCurrentPosition().then((position) => {
let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
//let latLng = new google.maps.LatLng(40.987469, 29.027119);
let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
}).catch((error) => {
console.log('Error getting location', error);
});
I am trying to check if user's position is available or not. When the user's gps is open, the code above works. But I need to check if GPS is not open and I will set a location by myself when I cannot get the location of the user. How should I check if the user location available or not ? I am using ionic-native geolocation. Thanks in advance.
You can use Diagnostic cordova plugin.
Diagnostic.isLocationEnabled().then( (avail) => {
console.log(avail);
if(!avail){
return Diagnostic.switchToLocationSettings();
}
}).catch( (err) => {
console.log(err);
}).then( () => {
console.log('entered');
return this.getLocation().then(()=>{ //get location } )
})
You can get current position using this code. I was getting same problem. Resolved using this code:
if (navigator.geolocation) {
var options = {
timeout: 10000,
enableHighAccuracy: false
};
navigator.geolocation.getCurrentPosition(position=> {
console.info('Position : ' + JSON.stringify(position));
}, error => {
console.log(' Error : ' + JSON.stringify(error));
}, options);
} else {
// you can set your custom position here if not location found.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With