0

从本地地理编码器导入 NativeGeocoderReverseResult 时出错。我正在使用带电容器的 ionic4

从 '@ionic-native/native-geocoder/ngx' 导入 { NativeGeocoder, NativeGeocoderReverseResult, , NativeGeocoderOptions };除了 NativeGeocoderForwardResult 之外的所有内容都在导入且没有错误

import { Component } from '@angular/core';
import { Plugins, Capacitor, CameraSource, CameraResultType, CameraDirection, Camera, Geolocation } from '@capacitor/core';
import { NativeGeocoder, NativeGeocoderReverseResult, NativeGeocoderForwardResult, NativeGeocoderOptions } from '@ionic-native/native-geocoder/ngx';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

  selectedImage: string;
  coordinates: { lat: number; long: number; };
  resultValue: any;

  constructor(private mediaCapture: MediaCapture, private alertcontroller: AlertController, private nativeGeocoder: NativeGeocoder) {
    this.getLocation();
   }

  getLocation() {
    if (!Capacitor.isPluginAvailable('Geolocation')) {
      this.alertcontroller.create({header: 'could not fetch location'});
      return;

    }
    Plugins.Geolocation.getCurrentPosition().then(position => {
      this.coordinates = { lat: position.coords.latitude, long: position.coords.longitude};
      console.log('coordinates-oojj', this.coordinates);

    });
    const options: NativeGeocoderOptions = {
      useLocale: true,
      maxResults: 5
  };

    this.nativeGeocoder.reverseGeocode(52.5072095, 13.1452818, options)
    .then((result: NativeGeocoderReverseResult[]) => {
      this.resultValue = result;
      console.log('resultValue',this.resultValue)
      console.log(JSON.stringify(result[0]))}
    )

    .catch((error: any) => console.log(error));
  }


}

4

1 回答 1

2

NativeGeocoderReverseResult 不再是 Ionic 的一部分。插件 (native-geocoder) 已更新至 3.2.0。文档已弃用。建议使用新的导入语句:

import { NativeGeocoder, NativeGeocoderResult, NativeGeocoderOptions } from '@ionic-native/native-geocoder/ngx';

使用以下代码:

let options: NativeGeocoderOptions = {
      useLocale: true,
      maxResults: 5
  };

  this.nativeGeocoder.reverseGeocode(52.5072095, 13.1452818, options)
    .then((result: NativeGeocoderResult[]) => console.log(JSON.stringify(result[0])))
    .catch((error: any) => console.log(error));

  this.nativeGeocoder.forwardGeocode('Berlin', options)
    .then((result: NativeGeocoderResult[]) => console.log('The coordinates are latitude=' + result[0].latitude + ' and longitude=' + result[0].longitude))
    .catch((error: any) => console.log(error));

如您所见, NativeGeocorderReverseResultNativeGeocoderForwardResult都被替换为NativeGeocoderResult

有关更多信息,请参阅源代码:https ://github.com/ionic-team/ionic-native/blob/4de49c37dd9bd23799b089595db998ade34a9c88/src/%40ionic-native/plugins/native-geocoder/index.ts

于 2019-05-02T12:07:21.330 回答