Angular 6 - 将 Bing 地图添加到传单地图

Guy*_*afe 3 bing-maps leaflet angular

我正在使用leaflet-bing-layer插件,以便使用 Leaflet 添加基于 Bing 的地图。
因为我也在使用 OSM,所以我同时导入leafletleaflet-bing-layer. 导入语句如下:

import * as L from 'leaflet';
import * as B from 'leaflet-bing-layer';
Run Code Online (Sandbox Code Playgroud)

以及组件内部传单的用法LeafletMapComponent

constructor () {
    this.baseMaps = {
      OpenStreetMap: L.tileLayer ("https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png", {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>, Tiles courtesy of <a href="https://hot.openstreetmap.org/" target="_blank">Humanitarian OpenStreetMap Team</a>'
      }),
      BingMap: B.tileLayer.bing(LeafletMapComponent.BING_KEY, {type: 'AerialWithLabels'})
    };
  }
Run Code Online (Sandbox Code Playgroud)

不幸的是,lingBingMap: B.tileLayer.bing(...收到一个错误: 无法读取未定义的属性 'bing'

我没有在 Angular 和 Typescript 中找到任何 Bing 地图的工作示例,所以我想这里缺少一些东西。

对我做错了什么有任何想法吗?

tre*_*con 5

您应该按如下方式导入Leaflet-bing-layer

import * as L from 'leaflet';
import 'leaflet-bing-layer';
Run Code Online (Sandbox Code Playgroud)

然后,您可以按如下方式添加 Bing 平铺层。

L.tileLayer.bing(LeafletMapComponent.BING_KEY).addTo(map);
Run Code Online (Sandbox Code Playgroud)

这会给你一个类型错误

property 'bing' does not exist on type 'tileLayer' 
Run Code Online (Sandbox Code Playgroud)

但是您可以通过将 L 定义为自定义类型来克服此错误:

(L as any).tileLayer.bing(LeafletMapComponent.BING_KEY).addTo(map);
Run Code Online (Sandbox Code Playgroud)

注意:我不会在构造函数上创建地图。我会选择一个生命周期钩子方法,这样我就可以确保在加载视图后创建地图。