none
Layer Pushpin Count

    Question

  • I currently have functionality where a user can click a checkbox for a certain type of category (ex: gas station, campground, etc.). It will add a layer displaying pushpins that are within a certain range of their route. On the map, I will see 7 pushpins that pop up for that category. Is there an easy way to get the count of pushpins that show up for each layer added?
    Monday, March 18, 2019 4:04 PM

All replies

  • Hi John,

    You should be able to count the collection of pins. Are you using map.entities collection to load your pins?

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

    Sincerely,

    IoTGirl

    Tuesday, March 19, 2019 4:21 PM
    Owner
  • Hi IoTGirl,

    I'm using the Find Nearby Query API search: https://docs.microsoft.com/en-us/bingmaps/v8-web-control/map-control-concepts/spatial-data-services-module-examples/query-api/find-nearby-example.

    Basically, I'm looping through the route path and at every 25 points, I'm performing a nearby search query with a radius of 30 miles on that route path point. I will add a layer using the QueryAPIManager search function.

    This works fine, I just want to know if there is a way to get the count of all the pushpins that show up for each layer. 

    Here is what the code looks like:

    if (searchTerm == 'gs park') {
        searchFilter.push(new sds.Filter('GsParkType', 'eq', 'gs park'));
    } else if (searchTerm == 'private') {
        searchFilter.push(new sds.Filter('PublicPrivate', 'eq', searchTerm));
        searchFilter.push(new sds.Filter('GsParkType', 'eq', 'other'));
    } else if (searchTerm == 'public') {
        searchFilter.push(new sds.Filter('PublicPrivate', 'eq', searchTerm));
        searchFilter.push(new sds.Filter('GsParkType', 'eq', 'other'));
    }
    
    //Create a query to get nearby data.
    var queryOptions = {
        queryUrl: sdsGSParks,
        spatialFilter: {
            spatialFilterType: 'nearby',
            location: routePoint,
            radius: 50
        },
        filter: new sds.FilterGroup(searchFilter, sds.FilterLogicalOperator.and)
    };
    
    //Process the query.
    Microsoft.Maps.SpatialDataService.QueryAPIManager.search(queryOptions, map, function (data) {
        //Add results to the layer.
        layer.add(data);
    });

    Tuesday, March 19, 2019 7:34 PM
  • Hi John,

    I see filter work here but no pins.  Take a look at how these pins are pushed.  https://www.bing.com/api/maps/sdk/mapcontrol/isdk/lookupentities

    When you create your entity collection you can take a count of that collection to get the numbers.

    Sincerely,

    IoTGirl

    Monday, March 25, 2019 5:05 PM
    Owner
  • I was able to fix the layer pushpin load issue finally. Instead of changing the data[index].image in the for loop, I created a new pushpin set using the data and added that pushpin set to the layer. It is working well now!

    Here is the comparison in the code just for reference:

    Previous method (load issue):

    //Process the query.
    Microsoft.Maps.SpatialDataService.QueryAPIManager.search(queryOptions, map, function (data) {
    	for (var i = 0; i <= data.length - 1; i++) {
    		data[i].image = poiPinImg;
    	}
    
    	//Add results to the layer.
    	layer.add(data);
    
    	Microsoft.Maps.Events.addHandler(layer, 'click', function (e) {
    		pushpinClicked(e);
    		map.setView({ center: e.target.getLocation(), centerOffset: new Microsoft.Maps.Point(0, 300), zoom: map.getZoom() });
    	});
    }, null, false, function (status, message) {
    	alert('Search failure. NetworkStatus: ' + status);
    });

    New method:

    //Process the query.
    Microsoft.Maps.SpatialDataService.QueryAPIManager.search(queryOptions, map, function (data) {
    	var pushpins = [], pin;
    
    	for (var i = 0; i <= data.length - 1; i++) {
    		var dataLoc = Microsoft.Maps.Location.parseLatLong(data[i].metadata.Latitude + ',' + data[i].metadata.Longitude);
    		pin = new Microsoft.Maps.Pushpin(dataLoc, {
    			icon: poiPinImg
    		});
    		pin.metadata = data[i].metadata;
    		pushpins.push(pin);
    	}
    
    	//Add results to the layer.
    	layer.add(pushpins);
    
    	Microsoft.Maps.Events.addHandler(layer, 'click', function (e) {
    		pushpinClicked(e);
    		map.setView({ center: e.target.getLocation(), centerOffset: new Microsoft.Maps.Point(0, 300), zoom: map.getZoom() });
    	});
    }, null, false, function (status, message) {
    	alert('Search failure. NetworkStatus: ' + status);
    });
    I still haven't figured out the layer pushpin count due to duplication issues. Unfortunately, the pushpin count is not a priority to my team currently so I will not be able to address this for some time.
    • Edited by John_Bulli Tuesday, March 26, 2019 6:45 PM Address layer pushpin count issue
    Tuesday, March 26, 2019 6:42 PM
  • Hi John,

    I am glad you are using a pushpin collection now. Not sure where you are pushing the pin twice, is your old method still in the code somewhere? 

    Maybe looking at how the sample https://www.bing.com/api/maps/sdk/mapcontrol/isdk/deletepushpins uses the collection will help.

    Sincerely,

    IoTGirl

    Wednesday, March 27, 2019 4:32 AM
    Owner