【问题标题】:Reverse geocoding in angular google maps角度谷歌地图中的反向地理编码
【发布时间】:2018-05-10 20:08:33
【问题描述】:
getAddress( lat: number, lng: number ) {
    console.log('Finding Address');
    if (navigator.geolocation) {
      let geocoder = new google.maps.Geocoder();
      let latlng = new google.maps.LatLng(lat, lng);
      let request = { latLng: latlng };
      geocoder.geocode(request, (results, status) => {
        if (status === google.maps.GeocoderStatus.OK) {
          let result = results[0];
          let rsltAdrComponent = result.address_components;
          let resultLength = rsltAdrComponent.length;
          if (result != null) {
            this.address = rsltAdrComponent[resultLength - 8].short_name;
          } else {
            alert('No address available!');
          }
        }
      });
  }
  }

我正在尝试在我的 Angular 4 应用程序中使用反向地理编码。我正在使用 agm 将谷歌地图与角度应用程序集成。

在*.ts中声明google变量如下

declare let google: any;

但是当我使用时,我得到如下错误,

ERROR ReferenceError: google is not defined

请帮我解决问题。

这里是component.ts

declare let google: any;
@Component({
  selector: 'app-find-cycle',
  templateUrl: './cmp.component.html',
  styleUrls: ['./cmp.component.scss']
})

export class Cmp {
}

【问题讨论】:

  • 希望你已经声明声明 let google: any;上面的组件装饰器?
  • 是的,我做到了
  • 包含谷歌地图的api对吗?
  • 你需要使用地图加载功能,在里面调用getAddress(),之前google没有加载
  • @Ruben,你能给我举个例子吗?

标签: angular reverse-geocoding angular-google-maps


【解决方案1】:

地图加载完成后执行getAddress():

<agm-map (mapReady)="mapReady()" [latitude]="lat" [longitude]="lng" [zoom]="zoom" [styles]="styles">

mapReady() {
    this.getAddress(...);
}

或将private mapsAPILoader: MapsAPILoader, 添加到您的构造函数中

 this.mapsAPILoader.load().then(() => { ... });

【讨论】:

  • 谢谢,我知道了。我错过了将 agm-map 加载到我的组件中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 2016-10-16
  • 2014-04-06
相关资源
最近更新 更多