Bing React 中的地图组件

Bing Map component in React

我想创建组件以使用 TypeScript 在 React.js 中显示 bing 地图。
我知道 github 中有很多组件用于此目的。但我想自己从头开始创建这个组件。
我在 React 中创建了 class,并在 componentWillMount 函数中将脚本标记注入到我的 html:

head
    componentWillMount() {
        const script = document.createElement("script");
        var scriptURL = "<script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?key=" + this.props.apiKey + "' ></script>";
        const scriptText = document.createTextNode(scriptURL);

        script.appendChild(scriptText);
        document.head.appendChild(script);
    }

当我想创建地图后跟 this documentcomponentDidMount 函数中是这样的:

componentDidMount() {
        var map = new Microsoft.Maps.Map(this.mapElement);
    }

我收到这个错误:

Cannot find name 'Microsoft'.

我应该如何将 'Microsoft' 模块导入我的组件?

试试这个:

componentWillMount() {
    const script = document.createElement("script"); // Creates <script></script>
    script.type = 'text/javascript'; // Set type
    script.src = 'https://www.bing.com/api/maps/mapcontrol?key=' + this.props.apiKey; // Set source
    document.head.appendChild(script); // Add script to page
}

componentWillMount 将在 React 的下一个主要版本中弃用,最好将所有代码放在 componentDidMount

试试这个:

componentDidMount() {
    const script = document.createElement("script");
    script.type = 'text/javascript';
    script.src = 'https://www.bing.com/api/maps/mapcontrol?key=' + this.props.apiKey; 
    document.head.appendChild(script);

    script.onload = function() {
        this.map = new Microsoft.Maps.Map(this.mapElement);
    }
}

这是 React BingMaps 组件的极简实现,不依赖于 BingMaps 类型定义。

首先介绍加载 BingMaps API 和 Microsoft 类型的服务:

export interface MapWindow extends Window {
  Microsoft: any;
}

declare let window: MapWindow;
export let Microsoft: any;


export function loadBingApi(key?: string): Promise<void> {
  const callbackName = "bingAPIReady";
  let url = `https://www.bing.com/api/maps/mapcontrol?callback=${callbackName}`;
  if (key) {
    url += `&key=${key}`;
  }

  return new Promise((resolve, reject) => {
    const script = document.createElement("script");
    script.type = "text/javascript";
    script.async = true;
    script.defer = true;
    script.src = url;
    window[callbackName] = () => {
      Microsoft = window.Microsoft;
      resolve();
    };
    script.onerror = (error: Event) => {
      reject(error);
    };
    document.body.appendChild(script);
  });
}

这是一个接受 mapOptions 作为 props 的 Map 组件:

interface IMapProps {
    mapOptions?: any;
}

export default class BingMap extends React.Component<IMapProps, any> {
  private mapRef = React.createRef<HTMLDivElement>();

  public componentDidMount() {
    loadBingApi().then(() => {
      this.initMap();
    });
  }

  public render() {
    return <div ref={this.mapRef} className="map" />;
  }

  private initMap() {
    const map = new Microsoft.Maps.Map(this.mapRef.current);
    if (this.props.mapOptions) {
      map.setOptions(this.props.mapOptions);
    }
    return map;
  }
}

用法

<BingMap
    mapOptions={{
      center: [47.60357, -122.32945],
      credentials:
        "--BingMaps key goes here--"
    }}
/>

Here is a demo