none
Multiple Pushpins Click Events in V8 RRS feed

  • Question

  • I can't tell from the documentation whether this is still supported in V8 or has been deprecated.  In V7 you could have many pushpins on the map and you could add individual click events to each pushpin with a parameter that would help identify which pushpin was clicked.   I am having problem doing the same thing in V8.  It appears that only one click event is supported.  The documentation seems to indicate that in V8 the preferred way is to add multiple pushpins to a layer but I have not seen an example a layer with multiple pushpins and  how you can identify which pushpin on a layer was clicked from the layer click event.

    Can you please provide a sample of how to this in V8?

    Thursday, August 18, 2016 6:33 AM

Answers

  • In V7 the only way to create multiple click events to pushpins was to loop through and add a click event to each pushpin. This is still supported in V8. V8 also allows you to add shapes to a layer and then add a single event to the layer which applies to all shapes in the layer. V7 did however have a bug in the map events which also fired on shapes in the map. This bug has been fixed in V8. We have found that a small number of applications were unaware that this was a bug and had developed on top of this unexpected behavior which is not available in V8. From what I can gather from your question, this is likely what your application was doing.

    Using Layers is extremely simple. To add pushpins to a layer you use the add function and can either add in pushpins 1 by 1 or simply pass in an array of pushpins and it will handle it. Here are some samples:

    http://www.bing.com/api/maps/sdk/mapcontrol/isdk#addMultipleLayerItems+JS

    https://msdn.microsoft.com/en-us/library/mt750277.aspx


    [Blog] [twitter] [LinkedIn]

    Thursday, August 18, 2016 7:09 PM
  • The layer class does this already. Add a click event to the layer itself. In the event arg sent to the event handler there is a primitive property which is the pushpin that was clicked. Here is a quick code sample:

    var pushpins = Microsoft.Maps.TestDataGenerator.getPushpins(10, map.getBounds());
    
    var layer = new Microsoft.Maps.Layer();
    layer.add(pushpins);
    map.layers.insert(layer);
    
    Microsoft.Maps.Events.addHandler(layer, 'click', function(e){
    	var pin = e.primitive;
    	
    	//do something with the clicked pushpin.
    });


    [Blog] [twitter] [LinkedIn]

    Thursday, August 18, 2016 9:17 PM

All replies

  • In V7 the only way to create multiple click events to pushpins was to loop through and add a click event to each pushpin. This is still supported in V8. V8 also allows you to add shapes to a layer and then add a single event to the layer which applies to all shapes in the layer. V7 did however have a bug in the map events which also fired on shapes in the map. This bug has been fixed in V8. We have found that a small number of applications were unaware that this was a bug and had developed on top of this unexpected behavior which is not available in V8. From what I can gather from your question, this is likely what your application was doing.

    Using Layers is extremely simple. To add pushpins to a layer you use the add function and can either add in pushpins 1 by 1 or simply pass in an array of pushpins and it will handle it. Here are some samples:

    http://www.bing.com/api/maps/sdk/mapcontrol/isdk#addMultipleLayerItems+JS

    https://msdn.microsoft.com/en-us/library/mt750277.aspx


    [Blog] [twitter] [LinkedIn]

    Thursday, August 18, 2016 7:09 PM
  • Thank you for your reply and clarifications.  I have a V7 application that uses click events for pushpins as you described.  I am adding some new features and also porting to V8.  As you mentioned, it is easy to create layers in V8  and add and array of pushpins.  What I am not sure about are the choices for the click events on the pushpins on a layer.  If you have 20 pushpins on a layer after a search for POI, you can still add one click event per pushpin on the layer.  But it would be simpler if there was one click event for the layer that would identify which entity (in this case pushpin) was clicked on and then retrieving that entity by its id on index in the event handler. Is there a way of identifying the object from the layer click event?




    • Edited by RezaPourzia Thursday, August 18, 2016 9:05 PM
    Thursday, August 18, 2016 8:51 PM
  • The layer class does this already. Add a click event to the layer itself. In the event arg sent to the event handler there is a primitive property which is the pushpin that was clicked. Here is a quick code sample:

    var pushpins = Microsoft.Maps.TestDataGenerator.getPushpins(10, map.getBounds());
    
    var layer = new Microsoft.Maps.Layer();
    layer.add(pushpins);
    map.layers.insert(layer);
    
    Microsoft.Maps.Events.addHandler(layer, 'click', function(e){
    	var pin = e.primitive;
    	
    	//do something with the clicked pushpin.
    });


    [Blog] [twitter] [LinkedIn]

    Thursday, August 18, 2016 9:17 PM
  • Perfect!  e.primitive was exactly what I was looking for.  Thank you.

    Thursday, August 18, 2016 9:23 PM