React中的Bing Map组件

时间:2019-09-27 08:24:56

标签: reactjs components bing-maps

我想创建组件以使用TypeScript在React.js中显示bing映射。
我知道github中有一个非常有用的组件。但我想为自己重新创建此组件。
我在react中创建了一个类,并在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 document  在componentDidMount中的功能如下:

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

我收到此错误:

  

找不到名称“ Microsoft”。

如何将“ Microsoft”模块导入组件?

3 个答案:

答案 0 :(得分:1)

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);
    }
}

答案 1 :(得分:1)

这是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);
  });
}

这是一个Map组件,它接受mapOptions作为道具:

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

答案 2 :(得分:0)

尝试一下:

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
}