Info box for Bing Maps

bing-maps, javascript, web-applications

Solution

your code works, I have tried it, as long as you only call `createMapPin()` once. But judging by your code your intent is to call `createMapPin()` multiple times, and this will cause the `infobox` to stop behaving like you think it would. The reason is this line:

pinInfobox = new Microsoft.Maps.Infobox(pin.getLocation(),
            { title: 'My Pushpin',
                description: 'This pushpin is located at (0,0).',
                visible: false,
                offset: new Microsoft.Maps.Point(0, 15)
            });

since you did not declare `pinInfobox` as a local variable via the `var` keyword, it implicitly gets declared as a global variable. I don't think this is your intent, because as soon as you call `createMapPin()` again, `pinInfobox`, because it is a global variable, will get overwritten. So in effect, even though you keep on creating new infoboxes, there is only one instance of it and it just keeps on getting assigned new values. So if you click on a pushpin, the one infobox probably pops up somewhere off screen and you don't see it. I believe your intent is to have one infobox associated with each pushpin? To do this, you need to declare the info box as a local variable, like so:

var pinInfobox = new Microsoft.Maps.Infobox(pin.getLocation(),
        { title: 'My Pushpin',
            description: 'This pushpin is located at (0,0).',
            visible: false,
            offset: new Microsoft.Maps.Point(0, 15)
        });

You will also need to modify your event handlers to interact with the local version of the infobox object and not just the single global variable. The easiest way to do is to just declare your event handlers as anonymous functions in the scope of your `createMapPin()` function, and override the meaning of the this keyword using function binding:

Microsoft.Maps.Events.addHandler(pin, 'click', function (e) {
    this.setOptions({ visible: true });
} .bind(pinInfobox));
Microsoft.Maps.Events.addHandler(map, 'viewchange', function(e) {
    this.setOptions({ visible: false });
}.bind(pinInfobox));

so here is your updated code:

function createMapPin(latLong, sName) {
    var pinImg = "imagespins/Good.png";

    var pin = new Microsoft.Maps.Pushpin(latLong, {
        icon: pinImg,
        anchor: new Microsoft.Maps.Point(8, 8),
        draggable: true,
        width: 48,
        height: 48
    });

    pin.title = sName;

    var pinInfobox = new Microsoft.Maps.Infobox(pin.getLocation(),
        {
            title: 'My Pushpin',
            description: 'This pushpin is located at (0,0).',
            visible: false,
            offset: new Microsoft.Maps.Point(0, 15)
        });

    // Add handlers for the pushpin events
    Microsoft.Maps.Events.addHandler(pin, 'click', function(e) {
        this.setOptions({ visible: true });
    }.bind(pinInfobox));
    // Hide the infobox when the map is moved.
    Microsoft.Maps.Events.addHandler(map, 'viewchange', function(e) {
        this.setOptions({ visible: false });
    }.bind(pinInfobox));
    map.entities.push(pin);
    map.entities.push(pinInfobox);
    return pin;
}

Problem

I am currently working on a bing maps application and I can't seem to get the info box to appear when the user clicks on the pin. I have been following the SDK and It should be almost exactly the same. Heres what I have: ``` // *********************************** // Function creates a single pin // and returns a reference to the pin function createMapPin(latLong, sName) { var pinImg = "imagespins/Good.png"; var pin = new Microsoft.Maps.Pushpin(latLong, { icon: pinImg, anchor: new Microsoft.Maps.Point(8, 8), draggable: true, width: 48, height: 48 }); pin.title = sName; pinInfobox = new Microsoft.Maps.Infobox(pin.getLocation(), { title: 'My Pushpin', description: 'This pushpin is located at (0,0).', visible: false, offset: new Microsoft.Maps.Point(0, 15) }); // Add handlers for the pushpin events Microsoft.Maps.Events.addHandler(pin, 'click', displayInfobox); // Hide the infobox when the map is moved. Microsoft.Maps.Events.addHandler(map, 'viewchange', hideInfobox); map.entities.push(pin); map.entities.push(pinInfobox); return pin; } function displayInfobox(e) { pinInfobox.setOptions({ visible: true }); } function hideInfobox(e) { pinInfobox.setOptions({ visible: false }); } ``` The pins get created and added to the map successfully and when the user clicks on the pin it does enter the dispayInfoBox method but the messagebox just doesnt seem to appear. Any suggestions are greatly appreactiate. Thanks!

Original source