-1

I figured an issue, while i have thousands of pins over the map, i am using drawing tool to draw shapes free hand and then executing the Intersection on "drawingEnded" event, While i could see the intersection should return more than it actually returns,

Am i missing something ? For Example, If there are around 500 pins under the new area drawn, Intersection method only returns 100 or few more,

My Spider Cluster Configuration: ` Microsoft.Maps.loadModule(['SpiderClusterManager'], function () {

            spiderManager = new SpiderClusterManager(map, pinssame, {

                //clusteredPinCallback: function (cluster) {
                //    //Customize clustered pushpin.
                //    cluster.setOptions({
                //        color: 'red',
                //        icon:'https://www.bingmapsportal.com/Content/images/poi_custom.png'
                //    });
                //},
                pinSelected: function (pin, cluster) {
                    if (cluster) {
                        showInfobox(cluster.getLocation(), pin);
                    } else {
                        showInfobox(pin.getLocation(), pin);
                    }
                },
                pinUnselected: function () {
                    hideInfobox();
                },
                gridSize: 80

            });
        });

`

Intersection Function Code which gets triggered after "drawingEnded" event: ` function findIntersectingData(searchArea) { //Ensure that the search area is a valid polygon, should have 4 Locations in it's ring as it automatically closes. if (searchArea && searchArea.getLocations().length >= 4) {

            //Get all the pushpins from the pinLayer.
            //var pins = spiderManager._data;

            //Using spatial math find all pushpins that intersect with the drawn search area.
            //The returned data is a copy of the intersecting data and not a reference to the original shapes, 
            //so making edits to them will not cause any updates on the map.
            var intersectingPins = Microsoft.Maps.SpatialMath.Geometry.intersection(pins, searchArea);
            //The data returned by the intersection function can be null, a single shape, or an array of shapes. 
            if (intersectingPins) {
                //For ease of usem wrap individudal shapes in an array.
                if (intersectingPins && !(intersectingPins instanceof Array)) {
                    intersectingPins = [intersectingPins];
                }
                var selectedPins = [];
                //Loop through and map the intersecting pushpins back to their original pushpins by comparing their coordinates.
                for (var j = 0; j < intersectingPins.length; j++) {
                    for (var i = 0; i < pins.length; i++) {
                        if (Microsoft.Maps.Location.areEqual(pins[i].getLocation(), intersectingPins[j].getLocation())) {
                            selectedPins.push(pins[i]);
                            break;
                        }
                    }
                }
                //Return the pushpins that were selected.
                console.log(selectedPins);
                return selectedPins;
            }
        }
        return null;
    }

`

The function is not returning accurate pin data, Am i missing something here ?

Any Help Appreciated,

Thanks & Regards, Shohil Sethia

UPDATE :

Just figured, It is an assumption ,I have multiple pins with same coordinates over the layer, Is this the reason that it returns only pins which intersects with different coordinates over the map ?,

Thanks & Regards, Shohil Sethia

SHOHIL SETHIA
  • 2,187
  • 2
  • 17
  • 26

2 Answers2

1

The method returns objects that represent the intersection, not the exact copies of input shapes. So yes, if multiple pushpins with the same coordinates are within the area, only one pushpin of that coordinates will be in the result, since that alone is good enough as a representation.

You can try the sample below, only one pushpin is returned:

// Creates a polygon of current map bounds
var polygon = new Microsoft.Maps.SpatialMath.locationRectToPolygon(map.getBounds());

// Creates a bunch of the pushpins of the same coordinates(map center)
var pushpin1 = new Microsoft.Maps.Pushpin(map.getCenter());
var pushpin2 = new Microsoft.Maps.Pushpin(map.getCenter());
var pushpin3 = new Microsoft.Maps.Pushpin(map.getCenter());
var pushpin4 = new Microsoft.Maps.Pushpin(map.getCenter());
var pushpin5 = new Microsoft.Maps.Pushpin(map.getCenter());

// Adds the shapes to map for some visualization
map.entities.push([polygon, pushpin1, pushpin2, pushpin3, pushpin4, pushpin5]);

// Only one pushpin is returned as result
var intersectingPin = Microsoft.Maps.SpatialMath.Geometry.intersection([pushpin1, pushpin2, pushpin3, pushpin4, pushpin5], polygon);

Have you checked if the number of results adds up when taking duplicate pins into account?

J S
  • 1,068
  • 6
  • 7
  • It does not add up, Expecting some 300, it returns about 100 or few more, I am trying to find an alternate solution to this, Is there any ? – SHOHIL SETHIA Aug 30 '18 at 09:53
  • Have you identified any sample pushpins whose coordinates should be in the area polygon but not in the intersection result? If so can you create sample code of that scenario? – J S Aug 30 '18 at 18:09
  • i got a solution, please refer to the answer and post your review on that. – SHOHIL SETHIA Aug 30 '18 at 18:26
0

I got a solution, Since Intersection API ignore multiple pushPins with same coordinates, Therefore there is another API named as contains which takes two parameters which are the shape and the pushpin, and it returns whether it is contained in that shape or not in a boolean form. So true if pushpin is in that shape, and false in the other way.

        function findIntersectingData(searchArea) {
        //Ensure that the search area is a valid polygon, should have 4 Locations in it's ring as it automatically closes.
        if (searchArea && searchArea.getLocations().length >= 4) {
            var selectedPins = [];
            for (var i = 0; i < pins.length; i++) {
                if (Microsoft.Maps.SpatialMath.Geometry.contains(searchArea, pins[i])) {
                    selectedPins.push(pins[i]);
                }
            }
            //Return the pushpins that were selected.
            console.log(selectedPins);
            //return updatePrescriberTerr(selectedPins);
            return selectedPins;
        }
        return null;
    }

Therefore in the above function the we can loop it from the pushPins array and form the intersection set accordingly based on the boolean values.

Hope it helps to those with similar scenario !

Regards,

Shohil Sethia

SHOHIL SETHIA
  • 2,187
  • 2
  • 17
  • 26