0
votes

I am trying to use the Bing Maps v8 control. I am getting no errors, but the div that's supposed to render the map is empty.

I am using React. Here's my code for the root component:

return (
      <div>
        <BingMap />
      </div>
    );

Then I have a utility function that'll load scripts when called:

const loadDynamicScript = (config, callback) => {
    const existingScript = document.getElementById(config.id);

    if (!existingScript) {
      const script = document.createElement('script');
      script.src = config.link;
      script.id = config.id;
      document.body.appendChild(script);

      script.onload = () => {
        if (callback) callback();
      };
    }

    if (existingScript && callback) callback();
  };

  export default loadDynamicScript;

Then I have my BingMap component:

import * as React from 'react';
import scriptLoader from '../../utils/scriptLoader'

const config = {
    id: 'bingmaps',
    link: 'https://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=[mykey]'
}
declare global {
    interface Window { Microsoft: any; GetMap: any }
}

window.Microsoft = window.Microsoft || {};
window.GetMap = window.GetMap || {}; 


export default class BingMap extends React.Component<{}, {}> {
     myRef: any;

     GetMap = () => {
      new window.Microsoft.Maps.Map(this.myRef, { });


    componentDidMount() {
        console.log(this.myRef)
        window.GetMap = this.GetMap;
        scriptLoader(config, null)
    }

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

When my component is mounted, it calls the script loader and loads the Bing maps, then executes my callback function. I am not getting any errors in the console.

Here is the generated div:

<div class="map" id="test" style="height: 100%; position: relative;">

<div class="MicrosoftMap" style="position: absolute; width: 100%; height: 100%; left: 0px; top: 0px;">
<div style="position: absolute; overflow: hidden; width: 100%; height: 100%; top: 0px; left: 0px;">
</div>
<div style="position: absolute; overflow: hidden; width: 100%; height: 100%; top: 0px; left: 0px; pointer-events: none; transform: scale(1);">
</div></div></div>

Any suggestion is appreciated.

1

1 Answers

0
votes

It may be of CSS issue, try to give some height and width to BingMaps component's parent div

<div style={{ height: '100vh', width: '100vw' }}>
    <BingMaps />
</div>

Try as above