Select features on initialize.
Closed this issue · 5 comments
I have a map that's being reloaded frequently and needs to persist data between instances. I can't seem to find any exposed method for selecting features as they're loaded. Is there any functionality like that somewhere in the library that I could access somehow? Or, if not, do you think it would be reasonably straightforward to add that functionality?
You can try using the filter method of the config object. The filter method is called for each Vector Tile feature as it is being parsed, but before it's drawn. This is an opportunity to alter the properties of the feature, or choose to not have it displayed (by returning False).
Try using vtf.select() to make it selected. I saw, however, that the select() method causes the feature to redraw, which (in this case) we don't want since the features haven't even drawn the first time.
Another thing to try is to set the selected property to true, and see if that works for you.
filter: function(vtf){
//Modify the feature's properties before draw.
vtf.properties.theme = theme;
vtf.properties.myMadeUpProperty = "blue";
if(vtf.properties.size = "large"){
//Try this first:
vtf.selected = true;
//Try this if that doesnt' work:
vtf.select();
}
return true; //draw feature if true. To hide feature, return false;
},
Thanks so much for the input! I'm still not having any luck though using this approach. Setting the 'selected' property had no effect, and when I try calling select() on the feature I get this exception:
Uncaught TypeError: feature.select is not a function
This is all that's going on in my code...
filter: function(feature) {
feature.select();
return true;
},
When I inspect the properties of 'feature' it appears several are missing compared to when I inspect a feature in the MVT handleClickEvent() method, or in the toggle() method (which appears to call select() successfully). Do you know why that would be?
Hmm. The only other think I can think of is to try waiting until the tiles load, and then use the onTilesLoaded event to grab the collection of features, and then call .select() on the ones you want to select.
Pulling from here: https://github.com/AmericanRedCross/GeoForce/blob/74359575cbd3ecf607c1f1924525e3e03db4780c/GeoAngular/app/scripts/controllers/map.js#L575
Something like
`layer.options.onTilesLoaded = function(){
//Now grab your layer's features
var features = layer.getLayers()[0]; //<--if you only have a single vector tile layer in your PBFs.
//Loop thru your features, and when you see the ones you want to select, call .select() on them.
features.forEach(function(foo, bar){
if(foo.properties.color = "red"){
foo.select();
}
});
}
`
..where layer is your instantiated MVTSource object.
Hopefully this will work for you - Sorry I don't have a better answer. I haven't really looked at this stuff in the last year or so.
Good luck!
I really appreciate all of the feedback! I've had to switch tasks for the moment, but I'll get back to this as soon as possible and let you know how it goes. Thanks again!
This worked! I had to change the first line from layer.getLayers()[0]
to mvt.layers.<layername>.features
and loop through the features as an object rather than an array, but then it worked perfectly. Thank you so much for the help!