Google maps - InvalidValueError: setMap: not an instance of Map

Google maps - InvalidValueError: setMap: not an instance of Map

我的代码如下:

var mapLatLng = new google.maps.LatLng(27, -83)
 var mapOptions = {
 zoom: 8,
 center: mapLatLng,
 draggable: true,
 mapTypeId: google.maps.MapTypeId.TERRAIN,
 gestureHandling: ‘cooperative’,
 minZoom: 7
 }

 this.map = new google.maps.Map(document.getElementById(‘map_canvas’), mapOptions);

 this.drawingManager = new google.maps.drawing.DrawingManager({
 drawingMode: google.maps.drawing.OverlayType.POLYGON,
 map: this.map
 )}

在我的 html 我有

<div id =“map_canvas”></div>

我知道问题出在哪里,但不知道如何解决。我需要使用实用程序 class 将 google 地图 api 与地点库一起导入。但是,在另一个实用程序 class 中,我必须从 google 地图 api 导入绘图和几何库。

我必须这样做,因为前者包含我的应用程序所需的组件,但前者没有我也需要的绘图和几何库。我无法编辑以前的实用程序 class。如果可以的话,我会向其中添加绘图和几何库。

我唯一能让它工作的方法是导入 google 地图 api 两次。问题是我从标题中得到了这个 setMap 错误。无论如何,我可以在两次导入 google 地图 api 时解决这个问题吗?

两次调用地图API可能会导致错误,例如这个JS:

You have included the Google Maps API multiple times on this page. This may cause unexpected errors.

例如参见

您应该能够修改现有的 JavaScript API 调用以包含额外的库。就像这样,只调用一次 API 并在请求中包含所有这些库,如下所示:

https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places,drawing,geometry

希望对您有所帮助!