Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 Geolocation set another location when position not available

    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.

like image 982
burc Avatar asked Feb 19 '26 18:02

burc


2 Answers

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 } )

  })
like image 144
raj Avatar answered Feb 21 '26 15:02

raj


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.
    }
like image 36
Girish Rathod Avatar answered Feb 21 '26 14:02

Girish Rathod