måndag 6 januari 2014

Extending Bing Maps Pushpin with Attributes

Recently I needed to store attributes with Pushpins in Bing Maps. Either I'm blind or it is just not there. Bing Maps is offering the following properties with the Pushpin object:

If I just try to add a property when initializing the Pushpin, Bing Maps will not take notice of that property due to the setOptions function on the Pushpin object. I don't really want to override the setOptions function - since I don't know the domino effect. So there are still two ways to solve the problem.

  1. Add a property on the Pushpin object - it is Javascript. Such as.
    var pin = new Microsoft.Maps.Pushpin(OpenSMILWebPart.map.getCenter(), { height: 50, width: 50 });
    and just add a property:
    pin.customfield = 'customData';
  2. Add get and set method on the Pushpin object such as:
    Microsoft.Maps.Pushpin.prototype.setAttributes = function () { this._attributes = arguments[0] }; Microsoft.Maps.Pushpin.prototype.getAttributes = function () { return this._attributes; };
    and set properties such as:
    pin.setAttributes({ customObject: 'some data' });
    use get method to receive data:
    pin.getAttributes();
Alternative two is more complex, but more useful if we need to control the data.

Microsoft - feel free to add a Tag property to each geometry type giving the opportunity storing values in.