multiple select controls on multiple layers in same map not working

classic Classic list List threaded Threaded
8 messages Options
crb
Reply | Threaded
Open this post in threaded view
|

multiple select controls on multiple layers in same map not working

crb
I have a map that when I add a single vector layer and associated SelectFeature control works fine, but when I add a 2nd vector layer and a SelectFeature control associated with it, neither SelectFeature control works.  I've seen other posts related to multiple SelectFeature controls on the same vector layer, but none on multiple layers so I'm not sure why it doesn't work.

If I keep the commented sections below, my SelectFeature works on the first layer added.  If I uncomment, neither SelectFeature works.

        var locsLayerDef = GetLayerDef('Monitoring Locations');  // creates 1st OpenLayers.Layer.Vector, applies styles, strategies, etc.
        var locSelectCtrl = GetSelectControl(locsLayerDef);  // creates 1st SelectFeature for vector layer
        //var selsLayerDef = GetLayerDef('selectedFeatures');  // creates 2nd OpenLayers.Layer.Vector, 
        //var selsSelectCtrl = GetSelectControl(selsLayerDef);  // creates 2nd SelectFeature for vector layer

        var mapOptions = GetDefaultMapOptions();

        var searchMap = new OpenLayers.Map('searchMap', mapOptions);

        SetBasemapLayers(initSettings.MapLayers);  // adds base layers to map

        searchMap.addLayer(locsLayerDef); // adds 1st vector 
        //searchMap.addLayer(selsLayerDef); // adds 2nd vector 
        
        AddDefaultMapControls(searchMap);  // adds PanZoomBar, LayerSwitcher, etc.
        AddFeatureSelectControl(searchMap, locSelectCtrl);  // does map.addControl(locSelectCtrl) and locSelectCtrl.activate()
        //AddFeatureSelectControl(searchMap, selsSelectCtrl);

crb
Reply | Threaded
Open this post in threaded view
|

Re: multiple select controls on multiple layers in same map not working

crb
I see now in the docs that having more than one selectable vector layer is unsupported. I can probably work around this, but am having trouble w/ another aspect of the solution I'm trying to implement, and that is having one vector layer (a "selected features" layer) display over the top of another "location features" layer. Even though the zIndex of the "selected features" layer is higher (as I can see when stepping through Map.js.setLayerZIndex()) it's still not displaying over the "location features" layer, even after a map.resetLayersZIndex().

Maybe there's another way around what I'm trying to do, but I think getting the one vector feature to display over the top of the other will work.

Ultimately the issue I'm trying to resolve is that having too many vector features in the map (hundreds at a time) slows the browser performance down too much (esp. IE, which is unusable).  We were originally loading all vector features on map init. We changed it so that:
   - vector features only display at a specific, zoomed-in scale
   - only the features in the current map extent are displayed (i.e. the callback to get the features is run on each map moveend event)
   - features use a cluster strategy

This all works fine.  Now the problem comes with selected features, which we'd like to show at *all* scales, since they should only ever be a small enough subset of all features that they don't affect browser performance, and they are visible to the user regardless of the scale that they switch to.  Originally we thought we would solve this by doing the following when a feature in the "location features" layer is selected:
1. clone the feature in the "location features" layer and add it to the "selected features" layer (which has a higher zIndex), hoping that the feature in the "selected features" layer would display over the top of the locations layer
2. show a popup on the feature (has to be the "location features" layer since only it can have an associated SelectFeatures control)

When unselecting the feature:
1. delete the cloned feature from the "selected features" layer
2. close the popup

As you can see, this only works if the "selected features" layer actually displays over the top of the "location features" layer.  Not sure yet if the 'selected' event will fire on the "location features" layer if the "selected features" layer (eventually) overlays it.

What's not working:
- when selecting a single (non-clustered) feature in the "location features" layer:
     * all features in that layer briefly disappear
     * the newly-added feature in the "selected features" layer briefly appears
     * the popup appears
     * the "location features" layer overlays the "selected features" layer
If I turn off the "location features" layer, I see this first feature (only) in the "selected features" layer
- only the first single selected feature is added to the "selected featues" layer: subsequently-selected single features are not (even though when debugging I can clearly see the call to 'selsLayerDef.addFeatures([feature])' executed)
- when clustered features in the "location features" layer are selected, they are not added to the "selected features" layer, even though when selecting them I can see the array getting added:
for (var i = 0; i < feature.cluster.length; i++) {
   hFeatures.push(feature.cluster[i].clone());
}
selsLayerDef.addFeatures(hFeatures); 
- on the SelectFeature control, both 'clickout' and 'toggle' are set to 'true', but when I do either in the map then the function associated with onUnselect does not fire

Should I be going about this a different way?
crb
Reply | Threaded
Open this post in threaded view
|

Re: multiple select controls on multiple layers in same map not working

crb
Something I forgot to mention, though I wouldn't think it should matter:  'displayInLayerSwitcher' is set to 'false' on my "selected features" layer.  It correctly doesn't show up in the layer switcher, but given its higher zIndex I would still expect it to render over the top of the "location features" layer.
Reply | Threaded
Open this post in threaded view
|

Re: multiple select controls on multiple layers in same map not working

Andy Deakin-2
In reply to this post by crb
Hi crb,

I might have missed your point, but this works for me to have multiple
layers selectable:

selectFeatures = new OpenLayers.Control.SelectFeature(
     [ layer1 , layer2 , layer3, layer4 ],
     {
         clickout: true,
         toggle: false,
         multiple: false,
         hover : true,
        onSelect : function(evt){
                // Show relevant popup
        },
        onUnselect : function(evt){
                // hide popup
        }
     }
);
map.addControl( selectFeatures );
selectFeatures.activate();


Hope this helps,

Andy

On 01/10/10 19:33, crb wrote:

> I see now  http://docs.openlayers.org/library/overlays.html#interaction in
> the docs  that having more than one selectable vector layer is unsupported.
> I can probably work around this, but am having trouble w/ another aspect of
> the solution I'm trying to implement, and that is having one vector layer (a
> "selected features" layer) display over the top of another "location
> features" layer. Even though the zIndex of the "selected features" layer is
> higher (as I can see when stepping through Map.js.setLayerZIndex()) it's
> still not displaying over the "location features" layer, even after a
> map.resetLayersZIndex().
>
> Maybe there's another way around what I'm trying to do, but I think getting
> the one vector feature to display over the top of the other will work.
>
> Ultimately the issue I'm trying to resolve is that having too many vector
> features in the map (hundreds at a time) slows the browser performance down
> too much (esp. IE, which is unusable).  We were originally loading all
> vector features on map init. We changed it so that:
>     - vector features only display at a specific, zoomed-in scale
>     - only the features in the current map extent are displayed (i.e. the
> callback to get the features is run on each map moveend event)
>     - features use a cluster strategy
>
> This all works fine.  Now the problem comes with selected features, which
> we'd like to show at *all* scales, since they should only ever be a small
> enough subset of all features that they don't affect browser performance,
> and they are visible to the user regardless of the scale that they switch
> to.  Originally we thought we would solve this by doing the following when a
> feature in the "location features" layer is selected:
> 1. clone the feature in the "location features" layer and add it to the
> "selected features" layer (which has a higher zIndex), hoping that the
> feature in the "selected features" layer would display over the top of the
> locations layer
> 2. show a popup on the feature (has to be the "location features" layer
> since only it can have an associated SelectFeatures control)
>
> When unselecting the feature:
> 1. delete the cloned feature from the "selected features" layer
> 2. close the popup
>
> As you can see, this only works if the "selected features" layer actually
> displays over the top of the "location features" layer.  Not sure yet if the
> 'selected' event will fire on the "location features" layer if the "selected
> features" layer (eventually) overlays it.
>
> What's not working:
> - when selecting a single (non-clustered) feature in the "location features"
> layer:
>       * all features in that layer briefly disappear
>       * the newly-added feature in the "selected features" layer briefly
> appears
>       * the popup appears
>       * the "location features" layer overlays the "selected features" layer
> If I turn off the "location features" layer, I see this first feature (only)
> in the "selected features" layer
> - only the first single selected feature is added to the "selected featues"
> layer: subsequently-selected single features are not (even though when
> debugging I can clearly see the call to
> 'selsLayerDef.addFeatures([feature])' executed)
> - when clustered features in the "location features" layer are selected,
> they are not added to the "selected features" layer, even though when
> selecting them I can see the array getting added:
>
> for (var i = 0; i<  feature.cluster.length; i++) {
>     hFeatures.push(feature.cluster[i].clone());
> }
> selsLayerDef.addFeatures(hFeatures);
>
> - on the SelectFeature control, both 'clickout' and 'toggle' are set to
> 'true', but when I do either in the map then the function associated with
> onUnselect does not fire
>
> Should I be going about this a different way?
>
>    

_______________________________________________
Dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/openlayers-dev
crb
Reply | Threaded
Open this post in threaded view
|

Re: multiple select controls on multiple layers in same map not working

crb
Thanks for the reply Andy.  While it's great that the SelectFeature control will support multiple layers, what I need to do is have multiple SelectFeature controls (that call different onSelect and onUnselect functions).  From the docs, I don't think that is supported:  
"As of OpenLayers 2.7, there is no support for selecting features from more than a single vector layer at a time..."  (technically not accurate as you showed.  But then:
"... The layer which is currently being used for selection is the last one on which the .activate() method of the attached select feature control was called."  Which is what I think is preventing me from doing what I hoped to do.
Reply | Threaded
Open this post in threaded view
|

Re: multiple select controls on multiple layers in same map not working

Andreas Hocevar-2
Hi,

if you just need to differentiate your onSelect and onUnselect actions, why don't you look at the event object's layer property in your handlers? Based on the layer, you can then choose the appropriate action.

Regards,
Andreas.

On Oct 1, 2010, at 21:54 , crb wrote:

>
> Thanks for the reply Andy.  While it's great that the SelectFeature control
> will support multiple layers, what I need to do is have multiple
> SelectFeature controls (that call different onSelect and onUnselect
> functions).  From the docs, I don't think that is supported:  
> "As of OpenLayers 2.7, there is no support for selecting features from more
> than a single vector layer at a time..."  (technically not accurate as you
> showed.  But then:
> "... The layer which is currently being used for selection is the last one
> on which the .activate() method of the attached select feature control was
> called."  Which is what I think is preventing me from doing what I hoped to
> do.
> --
> View this message in context: http://osgeo-org.1803224.n2.nabble.com/multiple-select-controls-on-multiple-layers-in-same-map-not-working-tp5591394p5592688.html
> Sent from the OpenLayers Dev mailing list archive at Nabble.com.
> _______________________________________________
> Dev mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/openlayers-dev



--
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.

_______________________________________________
Dev mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/openlayers-dev
Reply | Threaded
Open this post in threaded view
|

Re: multiple select controls on multiple layers in same map not working

qmarqeva
In reply to this post by Andy Deakin-2
Hi guys,

I'm in the same situation, trying with multiple vector layers... Thanks for the solution i implement that and now i'm trying to identify the vector layers that was clicked for personalize the onselect function...

Thanks again..

crb
Reply | Threaded
Open this post in threaded view
|

Re: multiple select controls on multiple layers in same map not working

crb
In your OnSelect function that runs when a feature is selected, you'll want to test on the contents of feature.layer (i.e. CLASS_NAME or 'name' or something similar). Based on whatever the values of feature.layer are, call another handler function for each type/name of layer.