# Help initializing location (getCurrentLocation)

**URL:** <https://forum.ionicframework.com/t/help-initializing-location-getcurrentlocation/10823>\
**Category:** ionic-v1\
**Created:** [October 8, 2014, 4:31pm UTC](https://forum.ionicframework.com/t/help-initializing-location-getcurrentlocation/10823 "2014-10-08T16:31:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stache](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/stache/32/7004_2.png) [@stache](https://forum.ionicframework.com/u/stache)\
**Post date:** [October 8, 2014, 4:31pm UTC](https://forum.ionicframework.com/t/help-initializing-location-getcurrentlocation/10823/1 "2014-10-08T16:31:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stache](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/stache/32/7004_2.png) [@stache](https://forum.ionicframework.com/u/stache)\
**Post date:** [October 8, 2014, 5:34pm UTC](https://forum.ionicframework.com/t/help-initializing-location-getcurrentlocation/10823/2 "2014-10-08T17:34:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [October 8, 2014, 11:59pm UTC](https://forum.ionicframework.com/t/help-initializing-location-getcurrentlocation/10823/3 "2014-10-08T23:59:36Z")

</div>

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...`

```auto
.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();
  })

}])

```

![](https://sea1.discourse-cdn.com/ionicframework/uploads/default/4470/129ab4dc6858ad4a.PNG)

---

<div class="post-metadata">

**Author:** ![stache](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/stache/32/7004_2.png) [@stache](https://forum.ionicframework.com/u/stache)\
**Post date:** [October 9, 2014, 12:29am UTC](https://forum.ionicframework.com/t/help-initializing-location-getcurrentlocation/10823/4 "2014-10-09T00:29:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stache](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/stache/32/7004_2.png) [@stache](https://forum.ionicframework.com/u/stache)\
**Post date:** [October 10, 2014, 7:52pm UTC](https://forum.ionicframework.com/t/help-initializing-location-getcurrentlocation/10823/5 "2014-10-10T19:52:40Z")

</div>

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

any clues?

---

<div class="post-metadata">

**Author:** ![dichao](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@dichao](https://forum.ionicframework.com/u/dichao)\
**Post date:** [January 1, 2015, 11:51am UTC](https://forum.ionicframework.com/t/help-initializing-location-getcurrentlocation/10823/6 "2015-01-01T11:51:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stache](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/stache/32/7004_2.png) [@stache](https://forum.ionicframework.com/u/stache)\
**Post date:** [January 1, 2015, 2:29pm UTC](https://forum.ionicframework.com/t/help-initializing-location-getcurrentlocation/10823/7 "2015-01-01T14:29:14Z")

</div>

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. ![image](https://global.discourse-cdn.com/ionicframework/original/2X/b/bdcded3496ac61af9ae43b22a41eb17cd6c1e7bb.png)

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
