Compilation failure with simple geolocation example

I am trying to test the geolocation feature of ionic, as described in the documentation here: https://ionicframework.com/docs/v2/native/geolocation/

This is on

	Cordova CLI: 6.5.0 
	Ionic CLI Version: 2.2.1
	Ionic App Lib Version: 2.2.0
	ios-deploy version: Not installed
	ios-sim version: Not installed
	OS: Linux 4.4
	Node Version: v7.7.3
	Xcode version: Not installed

When trying the simplest example in this documentation,

      Geolocation.getCurrentPosition().then((resp) => {
        // resp.coords.latitude
        // resp.coords.longitude
      }).catch((error) => {
        console.log('Error getting location', error);
      });

compilation within “ionic serve” fails with

[14:47:26]  typescript: src/app/app.component.ts, line: 23 
            Property 'getCurrentPosition' does not exist on type 'typeof Geolocation'. 

      L23:        Geolocation.getCurrentPosition().then((resp) => {
      L24:          // resp.coords.latitude

[14:47:26]  transpile failed 

I tried to narrow it down by creating a simple ionic application from scratch as test case, but this fails too.
Code is available at: https://github.com/ajeans/ionic2-geolocation

Am I doing something obviously wrong here?

Thanks.

I am getting that too.

Don’t go off the type Geolocation, inject an instance instead:

constructor(geoloc: Geolocation) {
  geoloc.getCurrentPosition().then(() => {
  });
}
5 Likes

@jimmy_peckme @ajeans Could it be because you’re targeting an outdated version of Ionic Native? Here’s an example of using plugins with Ionic Native 3.x++

An example would be @ionic-native/geolocation

I have also same error my code is below

please help me…
Thank you

Thanks for the video.
My understanding after watching this (and reading the message from @rapropos) was that:

Anyway, I tried that on my test project and after a few tests, I now have it working in the browser !!
Thanks a lot everyone!!

If anyone still struggles to make this work, please look at https://github.com/ajeans/ionic2-geolocation and see if that works for you.

Would it make sense to update the Geolocation page in the official documentation?

Thanks,
Arnaud

Looks like it’s already been done.

Thanks, I have mine working now.