Help initializing location (getCurrentLocation)


#1

Hello

Let me preface this with I’ve been using ionic and phonegap for about 2 weeks. My issue is I can NOT figure out where the heck I’m to initialize the getlocation code.

I created a service my code (below) and I call it in my app.js on .run, it fires but I get two alerts one asking for permission from the app, and the other like this.
/var/mobile/Applications/157EB70D-4AA7-826E-690F0CBE0F/appname.app/www/index.html

can anybody help with this? I’m stuck! How do I prevent the double request. I created a service so I could call the method if user was to move locations and pull to refresh, thus only showing data relevant to their position.

this.Geo = function() {
  
        navigator.geolocation.getCurrentPosition(

            function (p) {
                localStorage.setItem('userPosition',JSON.stringify({lat: p.coords.latitude, lng: p.coords.longitude}));
                localStorage.setItem('latitude', p.coords.latitude);
                localStorage.setItem('longitude', p.coords.longitude);
                console.log(p.coords.latitude + " "+ p.coords.longitude);
                 
                }, 
            function(error){
                 if(error.code == 1){
                    alert('You have denied access to location');
                 } else {
                   alert('code: '    + error.code    + '\n' +
                    'message: ' + error.message + '\n');
                }
        });
        if(localStorage.getItem('userPosition') == null) {
            return false;
        } else {
            return true;
        }
    }

Thanks
Matt


#2

My double confirmation is not solved, but I’m a bit closer, does anybody else get double confirmation?

my app.js

.run(function($ionicPlatform, myService) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }

   //Service for Geo-location
     document.addEventListener("deviceready", myService.Geo(), false);
  });
})

Service

this.Geo = function() {
      
     navigator.geolocation.getCurrentPosition( onSuccess, onError, {enableHighAccuracy: true });

        function onSuccess(p) {
                    localStorage.setItem('userPosition',JSON.stringify({lat: p.coords.latitude, lng: p.coords.longitude}));
                    localStorage.setItem('latitude', p.coords.latitude);
                    localStorage.setItem('longitude', p.coords.longitude);
                    console.log(p.coords.latitude + " "+ p.coords.longitude);
                     return true;
        };


        function onError(error){
                 if(error.code == 1){
                    alert('You have denied app access to location');
                 } else {
                   alert('code: '    + error.code    + '\n' +
                    'message: ' + error.message + '\n');
                }
                return false;
        };
        
    }

thanks


#3

I’m glad you asked this because I had this problems and was just postponing fixing it.

The problem for me is that my controller was asking this before the Cordova ready event fires.

I fixed it by wrapping my first location request in $ionicPlatform.ready...

.controller("MapController", ["$scope", "$cordovaGeolocation", "$ionicPlatform", function($scope, $cordovaGeolocation, $ionicPlatform) {

  $scope.map = {
    defaults: {
      tileLayer: 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
      maxZoom: 18,
      zoomControlPosition: 'bottomleft'
    },
    center: {
      lat: 32.73,
      lng: -96.97,
      zoom: 12
    },
    markers : {}
  };

  $scope.locate = function(){

    $cordovaGeolocation
      .getCurrentPosition()
      .then(function (position) {
        $scope.map.center.lat  = position.coords.latitude;
        $scope.map.center.lng = position.coords.longitude;
        $scope.map.center.zoom = 15;

        $scope.map.markers.now = {
          lat:position.coords.latitude,
          lng:position.coords.longitude,
          message: "You Are Here",
          focus: true,
          draggable: false
        }
      }, function(err) {
        // error
      });

  };

  $ionicPlatform.ready( function() {
    $scope.locate();
  })


}])


#4

And your double prompt went away?

What is $cordovaGeolocation in your example, is that your custom service?

I have noticed when I was calling it in the controller the controller was firing first, so maybe I’ll try wrapping my platform ready inside the controller.

Matt


#5

I spoke too soon, I thought it did the trick, however I’m getting locationManager::didFailWithError (null) from the iOS sim

any clues?


#6

hey guy, did you solve this? I have the same problem of locationManager::didFailWithError (null) and have no idea how to solve it.


#7

yeah, both xCode and iOS Simulator have settings for locations.

Under xCode go to debug ->simulateLocaiton->choose location, also, a little arrow shows up on bar above the debug console.

then on the simulator go to debug -> location pick a location plug in some coordinates.

I’m not convinced that the xCode settings does anything but I check it none the less.

good luck