Interactive Mapping Blog

Mapping Solutions News

Virtual Earth Control 100% height problem – Solved! Well almost

Ok, so I was using the new asp.net Virtual Earth map control and looking to size it to have a 100% width and height. Like many of you out there I was experiencing the problem that I could only seem to apply the 100% width. As soon as I tried to apply the 100% height the control was not visible when the page loaded.

Well, we here at Earthware have continued to work at the problem and have managed to come up with a solution. If you put the map control within a div which is positioned absolute and has a height and width of 100% itself, then you should see it works as required. See below…

<div style="position:absolute;width:100%;height:100%;">
    <ve:Map ID="Map1" runat="server" Height="100%" Width="100%"
    ZoomLevel="4" onserverloadmap="Map1_ServerLoadMap"
    OnClientLoadMap="Map1_ClientLoadMap"
    OnClientResize="Map1_ClientLoadMap" />
</div>

However, as always things are not quite that simple!! From what we can see this works in Internet Explorer 7 and Firefox 3, but not in earlier versions of these browsers. We’ve therefore come up with a semi fix for this. If you set the height and width using the Map.Resize method in the code behind then this works on initial page load. We’ve used some javascript and hidden fields in the code in front to get the correct height and width, as below…

CODE IN FRONT:

<script type="text/javascript">
 function Map1_ClientLoadMap(sender, e)
 {
     if (document.documentElement &&
        (document.documentElement.clientWidth ||
         document.documentElement.clientHeight))
     {
       //For the fix for IE 6 and Firefox versions less than 3
       $get("clientwidth").value = document.documentElement.clientWidth;
       $get("clientheight").value = document.documentElement.clientHeight;
     }
 }
</script>

CODE BEHIND:

protected void Map1_ServerLoadMap(object sender, EventArgs e)
{
    ResizeMap();
}

private void ResizeMap()
{
    if (Request.Browser.Type == "IE6" ||
       (Request.Browser.Browser == "Firefox" &&
        Request.Browser.MajorVersion < 3))
    {
        Map1.Resize(Convert.ToInt16(clientwidth.Value), 
                    Convert.ToInt16(clientheight.Value));
    }
}

Unfortunately at this time we cannot get the resize events to work (the Resize event only fires if you manually resize the control, not if you resize the browser window) but do check back again soon as we are continuing to work on this.

Please download an example of a working case to of all of the above in action.

2 Responses to “Virtual Earth Control 100% height problem – Solved! Well almost”

  1. deuce4 Said on

    Didn’t work for me. The map opened wide and doesn’t resize at all with this code. Perhaps it is confiuration dependent? I use VS 2008 with all the latest Net 3.5 etc.

    I spent quite a few hours working on solutions similar to this one. Probably just best to wait for the next release of the control, though I have not yet seen Micorsoft awknowledge the problem.

    Definitely a bug.

  2. info Said on

    Thanks Deuce,

    We are working with the same framework etc as you except we are using .net 3.5 sp1 but I doubt that would make a difference. What browsers were your trying?

    I have reported that the community is having problems with this to the dev team so hopefully we might get a response

Leave a Reply