Ionic 2 Beta / Geolocation plugin not working

I’m following the steps at http://ionicframework.com/docs/v2/platform/geolocation/ to call the geolocation plugin:

import {Geolocation} from 'ionic/ionic';

export class Locations {
    
    constructor() {
        console.log('entering constructor');
        let options = {timeout: 10000, enableHighAccuracy: true};
        Geolocation.getCurrentPosition(options).then((position) => {
            let lat  = position.coords.latitude
            let long = position.coords.longitude
        }, (err) => {
        // error
        });

        //switching to Geolocation.getCurrentPosition to navigator.geolocation.getCurrentPosition
        //causes an error saying the 1st argument is not a function
    }
}

However, the code above results in an error that getCurrentPosition of undefined can’t be retrieved. Is the documentation for Geolocation out of date or am I doing something wrong?

A few weeks late to the party, I know, but I just tried using Geolocation and ran into this same problem.

The documentation has a few problems with it. First, it tells you to use the ionic/ionic module. However, it’s not called ionic-framework/ionic.

Looking inside the ionic-framework/ionic.js, there is no reference to Geolocation.

Hopefully somebody else will see this bump and be able to point us in the right direction.

It appears that the geolocation plugin isn’t even in there. I did a full search of the code looking for Geolocation, and found no results.

Perhaps this has been documented, but not implemented yet? I’d love a confirmation either way on this topic.

It seems that the geolocation wrappers have been removed.

I ended up wrapping the cordova call into a promise:

    var p = new Promise(function(resolve, reject) {
        var options = {timeout: 10000, enableHighAccuracy: true};
        navigator.geolocation.getCurrentPosition(function (position) {
            resolve(position);
        }, function (err) {
            reject(err);
        }, options);    
    });
2 Likes

It appears that is our only option at the moment. Hopefully one more bump to the front page will catch the attention of somebody on the dev team who can shed some light if this feature is going to be baked in as their other posts/documentation suggest.

@tim - Sorry Tim, you’re the only person I’ve ever got a response from on Ionic 2 so I’m going to try to pull you in to help us :smile:

Hello! I’m actually not sure what the current status with plugins is, let’s ask @brandyshea or @mhartington :blush:

Hey guys! So we pulled out the plugin classes since it didn’t make sense to update ionic when we just need to fix a plugin class.

We’re currently working on ionic-native, which will be installed when you create a new app. This will allow the plugin classes to get updated and released at a faster pace than the framework.

But if you need to use a plugin thats not in ionic-native, you can always just use the window.PLUGIN-NAME api that is in each cordova plugin doc.

1 Like

Is ionic-native safe to use now, or should we implement geolocation ourselves until ionic-native is stable?

Can you add an example how to use?

my example that works there https://github.com/prolland006/ionic2-geoloc