【问题标题】:ionic GeoLocation retrieve place an address?ionic GeoLocation 检索地址?
【发布时间】:2018-12-12 09:23:30
【问题描述】:

如何从以下代码中检索经纬度的地址:

doLocate() {

    this.geolocation.getCurrentPosition().then((resp) => {
      // resp.coords.latitude
      // resp.coords.longitude
      this.datas = 'Lat: ' + resp.coords.latitude + '' + ' Lon: ' + resp.coords.longitude
    }).catch((error) => {
      console.log('Error getting location', error);
      });

  }

【问题讨论】:

  • 我知道了:获取位置时出错 PositionError 只允许安全来源

标签: android ionic-framework geolocation


【解决方案1】:

您必须将纬度和经度传递给另一个插件才能获取该位置的地址,请参阅Ionic Native Geocoder 以供参考。 或者你可以使用Google's Reverse Geocoding API

 import { NativeGeocoder, 
    NativeGeocoderReverseResult,
    NativeGeocoderForwardResult, 
    NativeGeocoderOptions } from '@ionic-native/native-geocoder';
import { Geolocation } from '@ionic-native/geolocation';

constructor(private nativeGeocoder: NativeGeocoder,private geolocation: Geolocation) { }


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

doLocate() {
    this.geolocation.getCurrentPosition().then((resp) => {
        this.datas = 'Lat: ' + resp.coords.latitude + '' + ' Lon: ' + resp.coords.longitude
        // then pass it to native geo coder plugin after you installed
this.nativeGeocoder.reverseGeocode(resp.coords.latitude, resp.coords.latitude, options)
  .then((result: NativeGeocoderReverseResult[]) => {
        console.log(JSON.stringify(result[0]))
    }).catch((error: any) => console.log(error));


    }).catch((error) => {
        console.log('Error getting location', error);
    });
} 

【讨论】:

  • 我试过了,但它不起作用....你能给我一个例子如何在这个或另一个代码中传递它吗?谢谢
  • @klejz0r 代码添加供参考,请尝试告诉我!
  • 我得到了这个:获取位置时出错 PositionError 只允许安全来源 – klejz0r
  • 你是从哪里得到这个错误的,是在 android 中运行 apk 还是在浏览器中使用 ionic serve 运行应用程序?
  • 在手机上...在浏览器上我有 cordova_not_available。
猜你喜欢
  • 2011-04-21
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2011-03-10
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多