Bing Maps Ajax 7: Adding InfoBox Support via Extension to Microsoft.Maps

30. November 2010

The new Bing Maps Ajax v7 control may be faster and smaller, but it is pretty light on features at the moment compared to its older brother; the v6.3 control. One of the features missing is support for showing InfoBox’s. Its simple to handle mouse events and display a <div> next to the pushpin, but I thought I’d take a little time to write a simple InfoBox extension to the new Microsoft.Maps namespace.

Here’s a screenshot of it in action:

BingMaps7_InfoBox_Screenshot

Usage

Let’s start with some sample usage of the InfoBox extension.

// Create Pushpin
var pushpin = new Microsoft.Maps.Pushpin(
    new Microsoft.Maps.Location(0,0)
);

// Create InfoBox
var infobox = Microsoft.Maps.Ext.InfoBox(
    "Title", /* <-- InfoBox Title to display */
    "Description", /* <-- InfoBox Description to display */
    map /* <-- A refernce to the Map where the InfoBox will be displayed */
);

// Add InfoBox to Pushpin
pushpin.setInfoBox(infobox);

// Add Pushpin to Map
map.entities.push(pushpin);

Usage of the “Microsoft.Maps.Ext.InfoBox” object is fairly simple. After creating the InfoBox object, you assign it to the Pushpin via the “setInfoBox” method that is also an extension that has been added.

CSS

There is a small bit of CSS code needed to style the InfoBox appropriately.

Here’s some sample HTML that is similar to what the InfoBox extension adds to the page for the InfoBox’s:

<div class="infobox">
<h4>Title</h4>
Description
</div>

The <div> element is given the CSS class of “infobox”. You use this class name to add CSS to your application to style the InfoBox’s as desired.

Here’s some sample CSS to give the InfoBox a style similar to the Bing Maps v6.x InfoBox’s:

<style>
.infobox
{
    position: absolute;
    z-index: 10000;
    
    width: 150px;
    
    background: #fff;
    border: solid 1px #555;
    padding: 8px;
    color: #555;
}
.infobox h4
{
    color: #000;
    margin: 0;
    font-weight: bold;
}
</style>

Custom Pushpin Method Reference

Here’s some documentation of the 2 methods I added to the Microsoft.Maps.Pushpin object:

Pushpin.setInfoBox(infobox)
This method is used to set a specific InfoBox to be show for the Pushpin.

Pushpin.removeInfoBox()
This method is used to remove a previously added InfoBox from the Pushpin.

InfoBox Method Reference

Here’s some documentation on the Microsoft.Maps.Ext.InfoBox object:

Microsoft.Maps.Ext.InfoBox(title, description, map)
This is the constructor of the InfoBox object.

  1. title – The HTML to show in the InfoBox title area.
  2. description – The HTML to show in the InfoBox description area.
  3. map – A reference to the Map where the InfoBox’s Pushpin is located, and where it will be displayed.

InfoBox.title(title)
This method gets or sets the Title of the InfoBox. If an argument is passed in, it will set the Title and return the InfoBox object to allow for a fluent interface. If no argument is passed in, it will return the value of the InfoBox Title.

InfoBox.descirption(description)
This method gets or sets the Description of the InfoBox. If an argument is passed in, it will set the Description and return the InfoBox object to allow for a fluent interface. If no argument is passed in, it will return the value of the InfoBox Description.

Full InfoBox Extension

Rather than add the InfoBox object to the root of the “Microsoft.Maps” namespace, I decided to add the “Microsoft.Maps.Ext” namespace where the InfoBox object will live. This will help keep it separated from “core” Microsoft.Maps functionality. Now the only methods added to the “core” namespace are the “setInfoBox” and “removeInfoBox” methods that were necessary to add directly to the Pushpin object.

Requirements:
This extension requires jQuery to function.

I could have coded the necessary JavaScript code to manipulate the HTML DOM myself, but it was just easier to use jQuery. Plus, if you’re like me, you will probably be using jQuery in your application anyway so it wont add any additional download.

Source Code:
Now for the real meat of the post. Here is the full JavaScript code for the InfoBox extension.

(function ($m) {
    /* $m == Microsoft.Maps namespace */
    /* Add "Microsoft.Maps.Ext" namespace if it doesn't exist
    This is used to hold the custom InfoBox functionality */
    if (!$m.Ext) { $m.Ext = {}; }

    /* Microsoft.Maps.Ext.InfoBox extension */
    var InfoBox = $m.Ext.InfoBox = function (title, desc, map) {
        return new InfoBox.fn.init(title, desc, map);
    };
    InfoBox.prototype = InfoBox.fn = {
        init: function (title, desc, map) {
            this._element = null;
            this._title = title;
            this._description = desc;
            this._map = map;
        },
        title: function (v) {
            if (v !== undefined) {
                this._title = v;
                return this;
            }
            return this._title;
        },
        description: function (v) {
            if (v !== undefined) {
                this._description = v;
                return this;
            }
            return this._description;
        },
        show: function (e) {
            /* Get location of Pushpin */
            var loc = this.map().
                tryLocationToPixel(
                    e.target.getLocation(), Microsoft.Maps.PixelReference.page
                );
            if (this._element === null) {
                /* Create <div> to show as InfoBox */
                this._element = $('<div>').
                    addClass('infobox').
                    appendTo($(document.body)).
                    html('<h4>' + this.title() + '</h4>' + this.description());
            }
            /* Show InfoBox and set position*/
            this._element.show().css({
                position: 'absolute',
                /* offset the infobox a little */
                top: loc.y - 10,
                left: loc.x + 10
            });
        },
        hide: function () {
            /* Hide InfoBox from view */
            if (this._element !== null) {
                this._element.hide();
            }
        },
        map: function (v) {
            if (v) {
                this._map = v;
                return this;
            }
            return this._map;
        }
    };
    InfoBox.fn.init.prototype = InfoBox.fn;

    /* Microsoft.Maps.Pushpin extensions */
    (function (proto) {
        if (!proto.setInfoBox) {
            proto.setInfoBox = function (infoBox) {
                this.removeInfoBox();
                this.infobox = infoBox;
                /* attach event handlers */
                this.infoboxMouseOverHandler = $m.Events.addHandler(
                    this,
                    "mouseover",
                    function (e) {
                        infoBox.show(e);
                    });
                this.infoboxMouseOutHandler = $m.Events.addHandler(
                    this,
                    "mouseout",
                    function (e) {
                        infoBox.hide(e);
                    });
            };
        }
        if (!proto.removeInfoBox) {
            proto.removeInfoBox = function () {
                /* detach event handlers */
                $m.Events.removeHandler(this.infoboxMouseOverHandler);
                $m.Events.removeHandler(this.infoboxMouseOutHandler);
                this.infobox = null;
            };
        }
    })($m.Pushpin.prototype);
})(Microsoft.Maps);

Conclusion

I didn’t go over the specifics of writing this plugin too much. If you want to see more examples of what’s involved, then I recommend reading the “Creating Infoboxes in Bing Maps AJAX v7” post on MSDN Social.

Bing Maps ,

Comments

12/8/2010 10:46:58 AM #
You write Nice articles,keep up good work.
12/9/2010 7:41:02 PM #
no i think you be wrong
12/12/2010 4:28:28 PM #
Thanks a lot for the information. I have been searching for this for awhile with Bing and it has been a real chore.
12/12/2010 6:31:22 PM #
I comply to the comment below fantastic blog post I must add
12/13/2010 1:01:49 AM #
I want to ask How can I make writing truly high-quality, preferred by millions of readers, arouse, give the impression of depth, touch the feelings, just a really special and extraordinary buildup
12/13/2010 1:54:41 AM #
Try not to think of certain food items as "off limits." When you ban particular food items or food items groups, it is natural to want those food far more, and then experience like a failure if you give in to temptation. If you are drawn towards sweet, salty or unhealthy meals, start by decreasing portion dimensions and not consuming them as typically. Later you may well uncover your self craving them less or thinking of them as only occasional indulgences.
12/13/2010 6:29:55 AM #
lol a couple of the reviews bloggers write are silly and unrelated, there are times i wonder whether they at all read the post before writing or whether they just look at the subject of the blog post and compose the very first thought that drifts to their minds. But it is great to see a intelligent commentary every now and then in contrast to the exact same, traditional blog bull which I too many times notice on the blogs. Regards PokemonCraft Admin.
12/13/2010 7:10:22 AM #
amazing site how can we contact you to help us improve our sites. http://greenpois0n.co.uk and http://www.nightclub.org.uk
12/13/2010 7:16:01 AM #
thank you. i found it. but it could have been more descriptive.
12/13/2010 12:39:12 PM #
I love Your blog. Keep writing!!
12/13/2010 9:28:56 PM #
i loved this, take a look at my blog http://brightestflashlights.com
12/13/2010 9:50:28 PM #
You really know how to keep a post interesting. Thank you very much.
12/13/2010 10:27:49 PM #
Who is the owner of this page? I would like to trade links.  Looking at the amount of hits that
  Bing Maps Ajax 7: Adding InfoBox Support via Extension to Microsoft.Maps
gets and it'd be worth it for me to trade links.  Would like to get ahead of the competition and all.  Also, most of my links come from web sites just like this place.
12/13/2010 10:29:49 PM #
Thanks for bothering to talk about this kind of, I feel strongly concerning this and love finding more on this subject subject. If possible, just like you gain expertise, would you mind bringing up-to-date your blog with considerably more? It is extremely helpful for me.
12/13/2010 10:43:04 PM #
I  admired your article. I think I am going to have to subscribe.And good Topic.
12/13/2010 11:54:05 PM #
Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I'm more of a visual learner,I found that to be more helpful. Just my my idea, Good luck  

Regards!


12/14/2010 12:36:36 AM #
I know that this post is the most piece of work that i've ever read in my life.
12/14/2010 2:11:16 AM #
why javascript error keep on receiving always when I do keep on trying to write a response
12/14/2010 2:23:27 AM #
I am really thankful to the author of this post for making this lovely and informative article live here for us. We really appreciate ur effort. Keep up the good work. . . .