我在我的项目中使用Angular 2 + Google Maps Places Autocomplete,它给了我这个错误:
错误错误:未捕获(承诺):ReferenceError:未定义谷歌
继承人html:
<agm-map id="googleMap">
</agm-map>
组件:
ngOnInit() {
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);
let directionsService = new google.maps.DirectionsService;
let directionsDisplay = new google.maps.DirectionsRenderer;
let mapProp = {
center:{lat: 30.42018, lng: -9.5981500},
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
let map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
directionsDisplay.setMap(map);
this.calculateAndDisplayRoute(directionsService, directionsDisplay);
//set google maps defaults
this.zoom = 4;
this.latitude = 39.8282;
this.longitude = -98.5795;
//create search FormControl
this.searchControl = new FormControl();
//set current position
//this.setCurrentPosition();
var options = {
types: ['(cities)'],
componentRestrictions: {country: "ma"}
};
//load Places Autocomplete
this.mapsAPILoader.load().then(() => {
let autocomplete = new
google.maps.places.Autocomplete(this.searchElementRef.nativeElement,
options);
});
}
模块:
imports: [
AgmCoreModule.forRoot({
apiKey: "MyOWNKEY",
libraries: ["places"]
}),
顺便说一句,我正在使用AGM(Angular Google Maps)
答案 0 :(得分:1)
declare var google.
将它放在组件的顶部。
此外,当然要确保您已在Angular的index.html
页面顶部添加了Google地图脚本