Skip to main content

How to: Change the Map Settings Dynamically

  • 2 minutes to read

This topic describes how to customize the MapViewer control used by the WebMapsListEditor to display raster maps in ASP.NET Web Forms applications. Here, it is assumed that you have an application created according to the Use Raster Maps tutorial.

Tip

If you do not need to change the map settings dynamically, you can use the Views | <ListView> | MapSettings node in the Model Editor (see IModelMapSettings).

Note

The complete example is available in the Feature Center demo (see c:\Users\Public\Documents\DevExpress Demos 24.2\Components\XAF\FeatureCenter\CS\FeatureCenter.Module.Web\Maps\MapController.cs)

The snippet below demonstrates the Controller that targets IMapsMarker List Views. It implements a SingleChoiceAction that allows users to select the map provider (Google or Bing). In this Actions’ SingleChoiceAction.Execute event handler, the WebMapsListEditor List Editor is accessed using the ListView.Editor property. Then, the MapViewer control is accessed using the WebMapsListEditor.MapViewer property. The control’s options (the MapSettings object) are available using the MapViewer.MapSettings property. And finally, the maps provider is changed using the MapSettings.Provider property.

using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Maps.Web.Helpers;
// ...
public class MapController : ViewController<ListView> {
    private SingleChoiceAction changeProviderAction;
    public MapController() {
        TargetObjectType = typeof(IMapsMarker);

        changeProviderAction = new SingleChoiceAction(
            this, "Map provider", PredefinedCategory.Edit);
        changeProviderAction.Items.Add(
            new ChoiceActionItem("Google", MapProvider.Google));
        changeProviderAction.Items.Add(
            new ChoiceActionItem("Bing", MapProvider.Bing));
        changeProviderAction.Execute += changeProviderAction_Execute;
        changeProviderAction.ItemType = SingleChoiceActionItemType.ItemIsMode;
    }
    protected override void OnActivated() {
        base.OnActivated();
        changeProviderAction.SelectedIndex = 0;
    }
    private void changeProviderAction_Execute(object sender, SingleChoiceActionExecuteEventArgs e) {
        WebMapsListEditor listEditor = View.Editor as WebMapsListEditor;
        if(listEditor != null) {
            listEditor.MapViewer.MapSettings.Provider = (MapProvider)e.SelectedChoiceActionItem.Data;
        }
    }
}

The image below demonstrates the Map provider Action.

Maps_MapProviderAction