我在一個Ionic Framework項目V3上,我嘗試使用getCurrentPosition
方法從@ ionic-native/geolocation實現Geolocation模塊,但沒有成功。請問您能否幫助我,我端給我一個錯誤file: 'file:///Users/emilio/Documents/Workspace/Ionic/my-places/src/pages/new-place/new-place.ts' severity: 'Erreur' message: 'Property 'getCurrentPosition' does not exist on type 'typeof Geolocation'.' at: '24,17' source: 'ts'
從@ ionic-native/geolocation實現地理位置
這是我的新place.ts文件:
import { Component } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { NavController } from "ionic-angular";
import { Geolocation } from '@ionic-native/geolocation';
import { PlacesService } from '../../services/places.service'
@IonicPage()
@Component({
selector: 'page-new-place',
templateUrl: 'new-place.html',
})
export class NewPlacePage {
// En attente au cas ou: public navCtrl: NavController, public navParams: NavParams
constructor(private placesService: PlacesService, private navCtrl: NavController) { }
onAddPlace(value: {title: string}) {
this.placesService.addPlace(value);
this.navCtrl.pop();
}
onLocateUser() {
Geolocation.getCurrentPosition()
.then(
(location) => {
console.log('Location fetched successfully')
}
)
.catch(
(error) => console.log('An error occured', error)
);
}
}
let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
});
這裏的HTML,我用推onLocatedUser()方法將數據轉換成<ion-col>
<ion-header>
<ion-navbar>
<ion-title>Add place</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<form (ngSubmit)="onAddPlace(f.value)" #f="ngForm">
<ion-item>
<ion-label>Title</ion-label>
<ion-input type="text" name="title" ngModel required></ion-input>
</ion-item>
<button ion-button block type="submit" [disabled]="!f.valid">Add a place</button>
</form>
<ion-grid>
<ion-row>
<ion-col><button ion-button block (click)="onLocatedUser()">Locate me</button></ion-col>
</ion-row>
<ion-row>
<ion-col>
<p>You location: </p>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
非常感謝我會試試這個,讓你知道它是怎麼回事... –