Hello,
We are working to sort the layers of our maps based on user preferences. I ran across the GeoKeyedCollection MoveUp, MoveTo, etc., but noticed these are not available in the MVC Edition. Is there an alternative way to do this same functionality in the MVC edition?
Currently we create each layer in an Overlay and add to an OverlayCollection for the map.
Thanks,
Chuck
GeoKeyedCollection MoveUp, MoveTo, etc. Functionality
I did find a solution to this by loading all the layers into a custom collection, applying a sort and then loading them into the OverlayCollection. I’m still curious though about the MoveUp, MoveDown, etc. though.
Thanks,
Chuck
Hi Chuck,
I think you can implement your custom MoveUp, MoveTo.
As below is a simple sample:
private void MoveUp(GeoKeyedCollection<Overlay> overlays, Overlay overlay)
{
if (overlays.Contains(overlay))
{
int index = overlays.IndexOf(overlay);
int count = overlays.Count;
if (index < count - 1)
{
overlays.Remove(overlay);
overlays.Insert(index + 1, overlay);
}
}
}
Regards,
Don
Thank you Don. I implemented something similar, but with a MoveTo functionality.
My issue now though is the re-sorting of my layers is not getting applied to the map. I am doing the re-sort in a MapActionFilter, passing in my Map.CustomerOverlays and re-sorting. After the re-sort is complete, I review the map.CustomOverlays and they are in the correct order, however once the map reloads, I’m not seeing the change.
After the MapActionFilter completes I call the Map.redrawBackgound(). The map redraws, but he order of the layers is the same. We do something similar where we change the formatting of a layer and call redrawBackgound() and that works fine.
Below is my code. Am I doing something wrong?
[MapActionFilter]
public
int
ReSortMapLayers(Map map, GeoCollection<
object
> args)
{
// get user settings
List<userlayersetting> userLayerSettings = userLayerSettingsBuilder.GetUserLayerSettings(Identity.UserID);
// loop through user settings
int
counter = 0;
foreach
(var setting
in
userLayerSettings)
{
// find matching layer in map
var overlay = map.CustomOverlays[setting.Layer];
if
(overlay !=
null
)
{
MoveTo(map.CustomOverlays, overlay, counter);
}
counter++;
}
return
0;
}
private
void
MoveTo(GeoKeyedCollection<overlay> overlays, Overlay overlay,
int
toIndex)
{
overlays.Remove(overlay);
overlays.Insert(toIndex, overlay);
}
Hi Chuck,
Today I did deeper research and found this function maybe only implemented in client side.
As below is a sample code about how to switch two overlays, I think that’s helpful for you to implement your MoveTo function in client side. Or you can put the JavaScript code in mapCallBack function, you can pass some useful information back like: target overlay id, target overlay index, move to index etc.
function switchOverlays() {
var sourceIndex;
var targetIndex;
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
if (layer.id == ‘sourceOverlay’) {
sourceIndex = i;
}
else if (layer.id == ‘targetOverlay’) {
targetIndex = i;
}
}
var tmp = Map1.layers[sourceIndex];
Map1.layers[sourceIndex] = Map1.layers[targetIndex];
Map1.layers[targetIndex] = tmp;
Map1.resetLayerZIndex();
}
Regards,
Don
Thanks Don. After trying several different approaches to sorting on the server side with no luck, I tried from the client-side and that did seem to work. For my purposes anyway, this is a better user experience.
Working in OpenLayers directly re-sorts the layers instantly:
map.setLayerIndex(map.getLayersByName(‘PLSS’)[0], 15)
Chuck
Hi Chuck,
I am glad to hear that help you solve your issue.
And thanks for your share about the solution.
Regards,
Don