none
V8 Mouse Events on Polylines in Layers RRS feed

  • Question

  • Polylines different behaviour to Pushpins?

    Using a Polyline in a layer the Target type is "polyline" (not 'layer') and the old V7 code still works.

    I thought the Polyline shape would behave like Pushpin shape in this regard (?) and the primitive would have to be used to get the Polyline properties.

    As in example below which does not work.

    if( e.targetType == "layer")
        {

    var shape = e.primitive;
    if (shape instanceof Microsoft.Maps.Polyline)
             { etc


    • Edited by blibber Monday, April 24, 2017 10:23 AM typo
    Monday, April 24, 2017 10:22 AM

Answers

  • That's not correct, I've gone through and tested all shapes in both release and experimental branch with all events and looked at the targetType value when the shape is in a layer, it's always "layer" which is to be expected. That said, there really is no need to check the target type when using a layer. It's more of a legacy thing for when you could only add events to individual shapes, but even then it wasn't needed.

    [Blog] [twitter] [LinkedIn]

    Wednesday, April 26, 2017 3:19 PM
  • Your code sample is adding the event to the polyline, not the layer, this the reason why the target type is polyline and not layer. Add the event to the polylayer.

    [Blog] [twitter] [LinkedIn]

    Sunday, April 30, 2017 7:47 PM
  • One click should result in one event handler being triggered. Can you provide more code that demonstrates what you are seeing. The event/map won't tell which segment of a polyline you clicked. If you want to be able to do that you have two options; 


    [Blog] [twitter] [LinkedIn]

    Monday, May 1, 2017 10:02 PM
  • Sounds like you are adding the event handler in a loop which would add the click event multiple times to the layer and thus fire it multiple times. This would also explain it works fine when adding the event to individual polylines. Pull the event code out of the loop and add it to the layer before or after the loop, usually this is done right after the layer is created.

    [Blog] [twitter] [LinkedIn]

    Wednesday, May 3, 2017 4:43 PM
  • You can add/remove the handler, or create a simple boolean flag and add an if statement inside your event handler, then you would only need to set that flag.

    [Blog] [twitter] [LinkedIn]

    Wednesday, May 3, 2017 7:13 PM

All replies

  • That's not correct, I've gone through and tested all shapes in both release and experimental branch with all events and looked at the targetType value when the shape is in a layer, it's always "layer" which is to be expected. That said, there really is no need to check the target type when using a layer. It's more of a legacy thing for when you could only add events to individual shapes, but even then it wasn't needed.

    [Blog] [twitter] [LinkedIn]

    Wednesday, April 26, 2017 3:19 PM
  • Sorry to disagree but that's not what happens here.

    I have

    polylayer.add(poly);
    MM.Events.addHandler(poly, "click", HandleClick);

    Then in HandleClick e.targetType has to be "polyline"

    function HandleClick(e)

    if( e.targetType == "polyline") { take actions here}

    And that works.

    e.targetType == "layer" does not

    Sunday, April 30, 2017 10:52 AM
  • Your code sample is adding the event to the polyline, not the layer, this the reason why the target type is polyline and not layer. Add the event to the polylayer.

    [Blog] [twitter] [LinkedIn]

    Sunday, April 30, 2017 7:47 PM
  • Absolutely right, my stupidity.

    However the next problem I have encountered using polylines with layers is that the Click Event fires repeatedly, apparently for every segment of a single polyline even though I have clicked only one.

    Is that how it works?

    And can one detect which segment has been clicked? That would be very useful.


    • Edited by blibber Monday, May 1, 2017 9:44 AM clarification
    Monday, May 1, 2017 9:42 AM
  • One click should result in one event handler being triggered. Can you provide more code that demonstrates what you are seeing. The event/map won't tell which segment of a polyline you clicked. If you want to be able to do that you have two options; 


    [Blog] [twitter] [LinkedIn]

    Monday, May 1, 2017 10:02 PM
  • I built a simple example and verified that it works correctly.

    But (adding the handler to the polyline layer) in my application the click handler fires multiple times (not equal to the number of segments as I suspected before, that is a red herring). 

    Adding the handler to the polyline on the other hand (as in V7) fires only once which is the behaviour I expected and want.

    Question: if you add the same click handler function multiple times to the layer does every instance fire? Perhaps that could be happening.

    I am happy with how it is working adding the handler to the polyline and am tempted to leave it working like that unless there are any disdvantages?

    On the segment question I was already using method 2, I just wondered if there was a 'built-in' better way.

    Wednesday, May 3, 2017 3:42 PM
  • Sounds like you are adding the event handler in a loop which would add the click event multiple times to the layer and thus fire it multiple times. This would also explain it works fine when adding the event to individual polylines. Pull the event code out of the loop and add it to the layer before or after the loop, usually this is done right after the layer is created.

    [Blog] [twitter] [LinkedIn]

    Wednesday, May 3, 2017 4:43 PM
  • If I add it once only I need a way to de-activate/re-activate it.

    At the moment I add or remove the Handler in several places, is there another (better) way?

    Wednesday, May 3, 2017 5:21 PM
  • You can add/remove the handler, or create a simple boolean flag and add an if statement inside your event handler, then you would only need to set that flag.

    [Blog] [twitter] [LinkedIn]

    Wednesday, May 3, 2017 7:13 PM
  • I used the boolean method which minimised the changes and all is now working as expected using polylines as a layer. Thank you
    Saturday, May 6, 2017 10:44 AM