ionic GeoLocation检索放置地址?

时间:2018-12-12 09:23:30

标签: android ionic-framework geolocation

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

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);
      });

  }

1 个答案:

答案 0 :(得分:0)

您必须将纬度和经度传递给另一个插件才能获取位置的地址,请参阅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);
    });
}