Cordova Geolocalization

Asked

Viewed 752 times

2

I’m having a very strange problem, I have an app running for a year and after a few months without updates when making a new release I realized that the geolocation was not working properly. When I use the getCurrentPosition function not from the error, however the returned object is empty. I made a new app just to test and the same problem occurred. I’m testing with Ionic.

I need an urgent solution. There has been some update on how to implement this recently?

Example:

inserir a descrição da imagem aqui

Upshot:

inserir a descrição da imagem aqui

As you can see in this basic example I just wanted to see the returned information, but for some reason I don’t know error however the object is empty.

2 answers

2

It’s not returning anything, because you put inside the

if(window.cordova && window.cordova.plugins.Keyboard)

Place out of the if, its function:

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    navigator.geolocation.getCurrentPosition(function(res) {
      console.log("deu certo...");
      console.log(JSON.stringify(res.coords));
    }, function(err) {
      console.log("deu erro...");
      console.log(JSON.stringify(err));
    })

    if (window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if (window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

I hope it helped you!

Hugs!

0

Try it that way:

    var positionOptions = {maximumAge:0, timeout: 30000, enableHighAccuracy: true};

    function gpsSuccess(obj){
      console.log('GPS Success latitude: ' + obj.coords.latitude);
      console.log('GPS Success longitude: ' + obj.coords.longitude);
    }

    function gpsError(obj){
      console.log('Gps error' + JSON.stringify(obj));
    }

    navigator.geolocation.getCurrentPosition(gpsSuccess, gpsError, positionOptions);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.