0

How can I view the coordinates returned in my template?

export class DashboardPage {
  constructor(public navCtrl: NavController) {
    Geolocation.getCurrentPosition().then(pos => {
        console.log(pos.coords.latitude, pos.coords.longitude);
    });
  }
}

I have tried:

export class DashboardPage {

  constructor(public navCtrl: NavController) {

    Geolocation.getCurrentPosition().then(pos => {
        this.latitude = pos.coords.latitude;
        this.longitude = pos.coords.longitude;
    });

  }

}

But this gives me an error:

Typescript Error
Property 'latitude' does not exist on type 'DashboardPage'.
src/pages/dashboard/dashboard.ts
Geolocation.getCurrentPosition().then(pos => {
this.latitude = pos.coords.latitude;
this.longitude = pos.coords.longitude;

I was hoping to be able to use {{ latitude }} and {{ longitude }} within my template

2 Answers 2

2

You need to declare your properties:

export class DashboardPage {

  latitude: number;
  longitude: number;

  constructor(public navCtrl: NavController) {

    Geolocation.getCurrentPosition().then(pos => {
        this.latitude = pos.coords.latitude;
        this.longitude = pos.coords.longitude;
    });
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to be more angular2/rxjs oriented:

export class DashboardPage {
  geoData$: Observable<any>;
  constructor(public navCtrl: NavController) {
    this.geoData$ = Observable.fromPromise(Geolocation.getCurrentPosition());
  }

}

and then in your view:

<div>lat: {{(geoData$|async)?.latitude}}</div>

2 Comments

Thanks. Do I need to import anything in particular at the top of my .ts file?
Depends on the rxjs version. Try it without and if it breaks, it should be under operators

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.