0

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.

Dan
  • 49
  • 1
  • 8

1 Answers1

0

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

Anand KS
  • 11
  • 5
  • Hi Anand, I don't think it's a CSS issue because the HTML divs are empty. It's not generating the right output. – Dan Mar 28 '19 at 18:08
  • I faced the same issue (which had empty HTML divs), but I noticed the parent
    of Map had no height property, so which in turn makes the Map to have no height. So when I set some height like say 500px for the parent
    , the map showed up.
    – Anand KS Mar 29 '19 at 08:34