【发布时间】:2018-01-31 19:23:20
【问题描述】:
我在我的项目中使用 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)
【问题讨论】:
-
看起来像一个导入问题...
-
我认为错误在于您的
new google.maps.LatLng(45.463688, 9.18814);行。把你的库导入,因为在这种情况下 vargoogle没有定义 -
我把库导入到哪里了??
-
我试图删除新的 google.maps.LatLng(45.463688, 9.18814);但它仍然给出同样的错误
标签: angular google-maps angular-google-maps