Search code examples
bing-mapspushpin

Attaching custom ID with pushpin in Bing maps


I want to add a unique ID number (taken from DB) to the pushpins I put on my Bing map. It works flawlessly when I do not use any themes (like Bing theme), like this:

var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(lat, lon), 
{icon: some_icon, width: 34, height: 43});

pushpin.customid = 13;
map.entities.push(pushpin);

And then I can later on access this "customid" property through Pushpin object.

BUT, when I activate a Bing theme, everything works, except these custom values, which are crucial to my application.

Any ideas on other ways of attaching some unique ID to these pushpins, or ideas for solving this issue?


Solution

  • I use a plain JavaScript object to wrap the Pushpin and its functionality, so that instead of storing custom properties on the Pushpin, I store them on the wrapper object.

    For example, assuming we have a map and location specified, we can then create our object:

    var Pin = function(location, map) {
        this._pushpin = new Microsoft.Maps.Pushpin(location);
        map.entities.push(this._pushpin);
        this.id = (new Date).valueOf();
        var self = this;
        Microsoft.Maps.Events.addHandler(this._pushpin, 'click', function() {
            console.log(self.id);
        })
    };
    

    This way, when we need to reference the id of the pushpin later, e.g. when the pushpin is clicked, then we can fetch the id of the wrapper object.

    It's a good practice to use the public API for extending the Bing Maps objects.

    Wrote a blog post about it here.