none
Events On MultiPolygon Shape RRS feed

  • Question

  • Hi,

    I have a need to add events to shapes in a layer, not at the layer level itself.  I figured this would be easy enough except that some of my shapes are multipolygons.  I tried to add the event on the shape that is a multipolygon but it throws an error in the microsoft js file.  Any ideas if this is possible?  It works on the layer level no problem but I have a need to use business logic to add the evens now.  Thanks.

    Friday, February 14, 2020 3:34 PM

Answers

  • MULTIPOLYGON returns a collection of Polygons.  So code something like this should work.

    var shape = 'MULTIPOLYGON(((-3.0 59,  -4 58, -2 57)),((-4 59,  -4 58, -5 57)))';
    var geometryCollection = Microsoft.Maps.WellKnownText.read(shape, { polygonOptions: { fillColor: 'rgba(0, 0, 255, 0.3)' } });
        for (var i = 0; i < geometryCollection.length; i++) {
            map.entities.push(geometryCollection[i]);
            Microsoft.Maps.Events.addHandler(geometryCollection[i], 'click', function () { highlight('polygonClick'); });
        }

    Saturday, February 22, 2020 1:42 AM

All replies

  • Hi JBrady412,

    There are a number of different event trapping samples on our interactive SDK and our samples site.  Would any of those options work for you?

    1. ISDK https://www.bing.com/api/maps/sdk/mapcontrol/isdk

    2. Samples https://bingmapsv8samples.azurewebsites.net/

    Sincerely,

    IoTGirl

    Thursday, February 20, 2020 9:24 PM
    Owner
  • Hi,

    Thanks for the response.  I am not having an issue setting the event up its more of is it possible to put an event on a shape that is of type multipolygon?  I am basically reading the well known text of shapes and after creating the shape I want to put the event on the shape and then add it to the layer.  Once I add the event it errors in a Microsoft js file.  I don't want to convert all multipolygons to polygons so was wondering if this was a limitation.

    So if i had something like:

    var test = 'MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)),((1 1, 3 1, 3 3, 1 3, 1 1)))';

    var shape = Microsoft.Maps.WellKnownText.read(test, geomStyles);

     Microsoft.Maps.Events.addHandler(shape, 'click', function () { highlight('polygonClick'); });

    Friday, February 21, 2020 2:53 PM
  • Hi JBrady412,

    I did reach out to the team with a link to this post but in the mean time, can you hook up the polygons then merge them rather than using a Multipolygon?

    Sincerely,

    IoTGirl

    Friday, February 21, 2020 11:21 PM
    Owner
  • MULTIPOLYGON returns a collection of Polygons.  So code something like this should work.

    var shape = 'MULTIPOLYGON(((-3.0 59,  -4 58, -2 57)),((-4 59,  -4 58, -5 57)))';
    var geometryCollection = Microsoft.Maps.WellKnownText.read(shape, { polygonOptions: { fillColor: 'rgba(0, 0, 255, 0.3)' } });
        for (var i = 0; i < geometryCollection.length; i++) {
            map.entities.push(geometryCollection[i]);
            Microsoft.Maps.Events.addHandler(geometryCollection[i], 'click', function () { highlight('polygonClick'); });
        }

    Saturday, February 22, 2020 1:42 AM
  • Hi,

    Thanks for asking the team for me, lookign forward to see what they say.  I would rather not have to break the polygons out of the collection, more for fears of storing them that way and or performance if doing it on the fly.

    Saturday, February 22, 2020 4:05 AM
  • Thanks can give it a shot jsut worried about the performance when I have a ton of shapes that are multipolygons to create the layer, i also will have to carry the metadata of the one multi to all the polygons in it which isnt ideal but could work.  Definitely still curious to just confirm adding the event on a mulitpolygon is an issue at all but this is worth trying so thanks very much.
    Saturday, February 22, 2020 4:12 AM
  • Hi JBrady,

    Your other option is to create a unioned Polygon that surrounds the polygon collection but I am not sure how that would effect the view you are hoping to achieve.

    Take a look at https://www.bing.com/api/maps/sdk/mapcontrol/isdk/binaryoperations and select the "union" button to see what I mean.

    Sincerely,

    IoTGirl

    Sunday, February 23, 2020 6:09 PM
    Owner
  • Hi,

    Thanks, can look into that as well.  Do you know if it was confirmed that the multipolygon itself isnt supported?

    Monday, February 24, 2020 6:33 PM
  • Hi JBrady,

    Yes, that is what Pramil confirmed for you above. Multipolygon is in fact not an individual object but an object collection so it does not support using a single click event.  You can iterate through and attach the same event to all polygons in the collection to get the behavior you want or try the method I suggested to get a boundary that contains all polygons and attach the event to that uber polygon.

    Sincerely,

    IoTGirl


    Tuesday, February 25, 2020 12:39 AM
    Owner
  • Ok thanks for your help

    Tuesday, February 25, 2020 3:11 PM
  • You are very welcome!
    Tuesday, February 25, 2020 9:06 PM
    Owner