none
Bing Maps v8 migration: replacing ClusteredEntityCollection with Clustering module - what to use for singlePinCallback RRS feed

  • Question

  • I'm working on migrating to Bing Maps v8 but I can't see any pushpins or clusters on the map. I noticed the singlePinCallback option is not available in the ClusterLayerOptions object as it was for ClusteredEntityCollection. Do you have any suggestions on how I can use the new ClusterLayer class but still achieve the same functionality that was being used in the singlePinCallback function? 

    // Old code

    Microsoft.Maps.loadModule("ClientSideClusteringModule", { callback: function () {
      this.layer = new ClusteredEntityCollection(mapsearch.map, {
     gridSize: 100,
     clusterPlacementType: 0,
     singlePinCallback: function (model) {
    return model.singlePin();
     },
     clusteredPinCallback: _.bind(function (models, center) {
    return this.clusterPin(models, center);
     }, this),
     clusteringEnabled: true
       });

    });

    // New code 

    Microsoft.Maps.loadModule('Microsoft.Maps.Clustering', function () {
    this.layer = new Microsoft.Maps.ClusterLayer(mapsearch.map, { 
    gridSize: 100,
    clusterPlacementType: 0,
    clusteredPinCallback: _.bind(function (models, center) {
     return this.clusterPin(models, center);
    }, this),
    clusteringEnabled: true
    });
    });

    // Part of code that is called on singlePinCallback with the old implementation of ClusteredEntityCollection

    module.cluster = Backbone.Model.extend({
    type: 'servercluster',
    serverCluster: true,

    initialize: function () {
      _.bindAll(this, 'singlePin', 'highlightPolygons', 'resetHighlight');

      this.Latitude = this.get('lat');
      this.Longitude = this.get('lng');
    },

    singlePin: function () {
      var count = this.get('count'),
    pin = new MM.Pushpin(this._LatLong, _.extend(pinDefaults.listing_cluster, {
     text: '' + count,
     typeName: 'pin-cluster' + mapsearch.getClusterPinSize(count)
    }));
      pin.model = this;
      this.pin = pin;

      if (config.showClusterBoundaries) {
    MM.Events.addHandler(pin, 'mouseover', this.highlightPolygons);
    MM.Events.addHandler(pin, 'mouseout', this.resetHighlight);
      }
      MM.Events.addHandler(pin, 'click', _.bind(function () {
    // alert('Captured single server cluster click for listing grid : ' + geohash.encode(this.get('lat'), this.get('lng'), this.collection.activeClusterZoom) + '... zooming in to box');

    var bbox = geohash.decode_bbox(geohash.encode(this.get('lat'), this.get('lng'), this.collection.precision));
    var bounds = MM.LocationRect.fromLocations(new MM.Location(bbox[0], bbox[1]), new MM.Location(bbox[2], bbox[3]));
    var opts = mapsearch.map.getOptions();

    opts.center = mapsearch.getCenterOffset(bounds.center);
    opts.zoom = mapsearch.determineZoom(bounds);
    mapsearch.map.setView(opts);
      }, this));

      return pin;
    },

    highlightPolygons: function () {
      if (this.polygons && this.polygons.length) {
    _.each(this.polygons, function (polygon) {
     polygon.setOptions(polygon.options.polygonHoverOptions);
    });
      }
    },

    resetHighlight: function () {
      if (this.polygons && this.polygons.length) {
    _.each(this.polygons, function (polygon) {
     polygon.setOptions(polygon.options.polygonOptions);
    });
      }
    }
    });

    Tuesday, June 6, 2017 8:33 PM

Answers

  • The open source clustering module used in V7 required the callback functions to return a pushpin through a callback. In V8 this isn't needed. Single pushpins will render the original pushpin you passed in. You can use the single pushpin callback to customize the pushpin using the setOptions function, but you don't need to return anything. You likely could simply remove this callback from your code.

    Also, the cluster pushpin callback provides a pushpin which you can customize using setOptions. There is no need to return this pushpin from the callback.


    [Blog] [twitter] [LinkedIn]

    • Proposed as answer by Ricky_Brundritt Tuesday, June 6, 2017 9:18 PM
    • Marked as answer by kmshel73 Tuesday, June 6, 2017 9:19 PM
    Tuesday, June 6, 2017 9:18 PM

All replies

  • The open source clustering module used in V7 required the callback functions to return a pushpin through a callback. In V8 this isn't needed. Single pushpins will render the original pushpin you passed in. You can use the single pushpin callback to customize the pushpin using the setOptions function, but you don't need to return anything. You likely could simply remove this callback from your code.

    Also, the cluster pushpin callback provides a pushpin which you can customize using setOptions. There is no need to return this pushpin from the callback.


    [Blog] [twitter] [LinkedIn]

    • Proposed as answer by Ricky_Brundritt Tuesday, June 6, 2017 9:18 PM
    • Marked as answer by kmshel73 Tuesday, June 6, 2017 9:19 PM
    Tuesday, June 6, 2017 9:18 PM
  • Thank you!
    Tuesday, June 6, 2017 9:20 PM