【发布时间】: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