Helena Standaert
Helena Standaert

Reputation: 569

navigator.geolocation is undefined

I'm creating an AngularJS app that I'm running in cordova to turn it into a mobile app. Now I want to use cordova's geolocation plugin, but obviously it returns undefined. I've added android as a platform and adjusted the permissions in the manifest and config.xml

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

android/res/xml/config.xml

<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>
<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>
<plugin name="Geolocation" value="org.apache.cordova.GeoBroker" />

In my Angular app.js, I've created the module and in the module.run, I'm calling the geolocation with following code:

 $rootScope.onSuccess = function(position) {
        alert('right here');
        $rootScope.pos= {
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            timestamp: position.timestamp
        }    
    };

     $rootScope.onError = function(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    navigator.geolocation.getCurrentPosition($rootScope.onSuccess(),   $rootScope.onError());

The strange thing is... It does enter the onSuccess function, since I'm seeing the alert. However, in my logcat, I get following error:

Uncaught TypeError: Cannot read property 'coords' of undefined at file:///android_asww/scripts/app.js:80

Am I missing something here? Thanks in advance HS.

Upvotes: 0

Views: 2197

Answers (1)

Raymond Camden
Raymond Camden

Reputation: 10857

You're passing $rootScope.onSuccess() to the geolocation success handler, which means, "I want you to run $rootScope.onSuccess() and the result is the success handler." You probably meant this instead: $rootScope.onSuccess (ie, remove the parens).

Upvotes: 1

Related Questions