在应用上显示国家名称

时间:2018-05-05 09:28:02

标签: google-maps ionic-framework ionic3

当我尝试在浏览器上查看我当前的国家/地区名称时,我在控制台上收到了cordova通知。 有什么方法或替代方法可以在我的tests.html上向我展示,比如输入字段(在浏览器中)? 另一方面,如果存在显示实际国家名称的另一种选择,请告诉我。

tests.ts

import { Component, ViewChild, ElementRef } from "@angular/core";
import { NavController, IonicPage, NavParams } from "ionic-angular";
import {
  NativeGeocoder,
  NativeGeocoderReverseResult
} from "@ionic-native/native-geocoder";
import { Geolocation } from "@ionic-native/geolocation";

declare var google;

@IonicPage()
@Component({
  selector: "page-tests",
  templateUrl: "tests.html"
})
export class TestsPage {
  @ViewChild("mapSmall") mapElement: ElementRef;

  map: any;
  message: any;
  location: any;
  gmLocation: { lat: number; lng: number } = { lat: 0, lng: 0 };

  constructor(
    public navCtrl: NavController,
    private geolocation: Geolocation,
    private nativeGeocoder: NativeGeocoder,
    public navParams: NavParams,
  ) {
    this.onLocateUser();
  }

  onLocateUser() {
    this.geolocation
      .getCurrentPosition()
      .then(location => {
        console.log(
          "position gotten: long:",
          location.coords.longitude,
          " lat:",
          location.coords.latitude
        );
        this.location = location;
        this.gmLocation.lat = location.coords.latitude;
        this.gmLocation.lng = location.coords.longitude;
        this.nativeGeocoder.reverseGeocode(location.coords.latitude,location.coords.longitude)
  .then((result: NativeGeocoderReverseResult) => console.log(JSON.stringify(result.countryName)))
  .catch((error: any) => console.log(error));
      })
      .catch(error => {
        console.log("Error getting location", error);
      });
  }

  ionViewDidLoad() {
    let latlng = new google.maps.LatLng(
      this.gmLocation.lat,
      this.gmLocation.lat
    );
    setTimeout(() => {
      this.loadMap(latlng);
      this.addMarker(this.gmLocation.lat, this.gmLocation.lng);
    }, 100);
  }

  ...

}

0 个答案:

没有答案