Bing Maps v7 Ajax Hacks: InfoBox Description containing Html tags

10. July 2011

For some odd reason the Bing Maps team decided to not allow HTML tags within the Description property of the InfoBox class they baked into the Bing Maps v7 Ajax control. I don’t know why they would limit it in such a way, but thankfully I have figured out a hack to override it and allow HTML tags as desired.

BingMaps7HackInfoBoxDescriptionHTML

Here’s a full sample page that adds a map with pushpin and infobox, and sets up overriding support to allow HTML tags within the infobox’s description:

 

<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
      <script type="text/javascript">
         var map = null;

         function GetMap()
         {
            // Initialize the map
            map = new Microsoft.Maps.Map(document.getElementById("myMap"), {credentials:"Bing Maps Key"}); 

            // Retrieve the location of the map center 
            var center = map.getCenter();
            
            // Add a pin to the center of the map
            var pin = new Microsoft.Maps.Pushpin(center, {text: '1'});

            // Create the info box for the pushpin
            var infobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0), {
                title: 'My Pushpin',
                description: 'A <i>short</i> description that contains ' +
                    'a couple <span style="color: red; font-weight: bold;">HTML</span> ' +
                    'tags, including a <a href="#">hyperlink</a>.',
                visible: true
            });

            // Add a handler for the pushpin click event.
            Microsoft.Maps.Events.addHandler(pin, 'click', function () {
                infobox.setOptions({ visible: true });

                // Override InfoBox Description to Allow the HTML tags it contains.
                infobox.cm1001_er_etr.descriptionNode.innerHTML = infobox.getDescription();
            });

            // Hide the info box when the map is moved.
            Microsoft.Maps.Events.addHandler(map, 'viewchange', function () {
                infobox.setOptions({ visible: false });
            });


            // Add the pushpin and info box to the map
            map.entities.push(pin);
            map.entities.push(infobox);

            // Now that the InfoBox has been added to the Map,
            // override the Description to allow HTML
            infobox.cm1001_er_etr.descriptionNode.innerHTML = infobox.getDescription();
         }
      </script>
   </head>
   <body onload="GetMap();">
      <div id='myMap' style="position:relative; width:500px; height:500px;"></div>
   </body>      
</html>
Warning: This is a hack and it relies on accessing part of the “non-public” API within the control. If the Bing Maps team decides to rename “cm1001_er_etr.descriptionNode” in a future release/update of the Bing Maps v7 control, then this code may stop working.

Bing Maps ,

Comments

7/11/2011 1:36:54 PM #
Bedankt voor dit  artikel. Ik vond het erg interressant!
7/11/2011 5:01:33 PM #
Nice read, I just passed this onto a friend who was doing some research on that
7/11/2011 5:56:35 PM #
I just cant quit reading this. Its so cool, so full of info that I just didnt know. Im glad to see that men and women are in fact writing about this issue in such a smart way, showing us all distinct sides to it. Youre a great blogger. Please keep it up. I cant wait to read whats next.
7/12/2011 4:18:10 AM #
<a href="www.geakeit.co.uk/.../installing-microsoft-sql-server-2005-on-windows-7-32-bit-and-64-bit.html">Installing Microsoft SQL Server 2005 on Windows 7</a> does not work when you try to do it following the normal steps. <a href="www.geakeit.co.uk/.../...-64-bit.html">SQL Server 2005 not working installed on Windows 7</a> is usual so for <a href="www.geakeit.co.uk/.../...-64-bit.html">sql server and sql management studio needs customised install</a>. <a href="www.geakeit.co.uk/.../...-64-bit.html">How to install sql server 2005 on windows 7</a> is a guide that shows you <a href="www.geakeit.co.uk/.../...-64-bit.html">how sql 2005 windows 7</a> works and <a href="www.geakeit.co.uk/...-and-64-bit.html">how to install sql server 2005 for windows 7</a>.
7/12/2011 6:29:41 AM #
When I open your RSS feed it puts up a bunch of trash, is the issue on my side?
7/12/2011 8:00:38 AM #
Nice blog! I am glad for writing to make you understand what a extraordinary discovery my friend's child had viewing your webblog. She noticed many things, not to mention what it is like to have a very effective giving style to let the mediocre ones with ease comprehend various tortuous things. You really surpassed her expected results. I appreciate you for providing these important, healthy, revealing and in addition fun guidance on your topic to Ethel. Thank u!
7/12/2011 1:58:30 PM #
You got a really useful blog I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me.
7/12/2011 2:16:34 PM #
Great post! I?m just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it?s all a bit new to us. The example above is something that I worry about as well, how to show your own genuine enthusiasm and share the fact that your product is useful in that case
7/12/2011 4:07:26 PM #
Congratulations been emulated
7/12/2011 4:15:51 PM #
was read to the end of the super
7/12/2011 4:28:17 PM #
was read to the end of the super
7/12/2011 4:44:40 PM #
much like the literature
7/12/2011 4:48:56 PM #
Congratulations on writing has been much emulated
7/12/2011 4:53:50 PM #
much like the literature
7/12/2011 5:41:00 PM #
much like the literature
7/12/2011 5:43:46 PM #
Hey there! Do you use Twitter? I'd like to follow you if that would be ok. I'm absolutely enjoying your blog and look forward to new updates.
7/12/2011 6:47:26 PM #
much like the literature
7/12/2011 6:56:18 PM #
much like the literature
7/12/2011 7:02:14 PM #
Congratulations been emulated
7/12/2011 8:46:23 PM #
This is probably the most awesome entry I have every read. If everyone read this, there would be half as less disappointments.
7/12/2011 10:48:54 PM #
I personally learn something much more demanding on various personal blogs each day. It will always become inspiring to study content from other writers and practice a little something from their store. I’d choose to make use of some of your respective articles on my blog site if you don’t mind. Of course I’ll supply you with a backlink on my web page. Many thanks for posting.
7/13/2011 1:38:12 AM #
It is my pleasure to be a merber in your biog,and thank you so much that you allow me to share the article,it is my true feeling that I want to share this with others.Thanks again for everything, but most especially your friendship.