Skip to main content
A newer version of this page is available. .

How to: Change the Map Marker Icon

  • 4 minutes to read

This topic describes how to access the dxMap widget directly using JavaScript code. This is for an application created according to the Use Raster Maps tutorial if you need to customize the widget options that are not mapped to properties of the IModelMapSettings node in the Model Editor. In this example, the markerIconSrc option specifying the custom icon to be used for the map marker is customized.

Change an Icon for All Markers

using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Maps.Web;
// ...
public class MarkerIconController : ViewController {
    public MarkerIconController() {
        TargetObjectType = typeof(IMapsMarker);
        TargetViewType = ViewType.ListView;
    }
    protected override void OnViewControlsCreated() {
        base.OnViewControlsCreated();
        WebMapsListEditor mapsListEditor = ((ListView)View).Editor as WebMapsListEditor;
        if (mapsListEditor != null) {
            mapsListEditor.MapViewer.ClientSideEvents.Customize =
            "function(sender, map) { map.option('markerIconSrc', 'http://js.devexpress.com/Demos/RealtorApp/images/map-marker.png'); }";
        }
    }
}

The result is demonstrated in the image below.

Maps_CustomMarker

Use an Individual Icon For Each Marker

Assume that you have the Store business object which implements IMapsMarker and exposes the IconPath property containing an icon URL.

To change an icon of each Store on a map, handle the MapViewerClientSideEvents.Customize client-side event of the MapViewer control. Use the MapViewerBase.CustomJSProperties event to serialize and pass markers data.

public class MarkerIconController : ObjectViewController<ListView, Store> {
    protected override void OnViewControlsCreated() {
        base.OnViewControlsCreated();

        WebMapsListEditor webMapsListEditor = ((ListView)View).Editor as WebMapsListEditor;
        if(webMapsListEditor != null) {
            webMapsListEditor.MapViewer.ClientSideEvents.Customize = GetCustomizeScript();
            webMapsListEditor.MapViewer.CustomJSProperties += MapViewer_CustomJSProperties;
        }
    }
    private void MapViewer_CustomJSProperties(object sender, DevExpress.Web.CustomJSPropertiesEventArgs e) {
        MapViewer mapViewer = sender as MapViewer;
        mapViewer.JSProperties["cpMarkerIcons"] = GetMarkerIcons(mapViewer.MapSettings);
    }
    private List<string> GetMarkerIcons(MapSettings mapSettings) {
        List<string> result = new List<string>();

        foreach(var marker in mapSettings.Markers)
            result.Add(GetIconPathForMarker(marker));
        return result;
    }
    private string GetCustomizeScript() {
        return @"function(sender, map) {{
                            var markerIcons = map.parent.cpMarkerIcons;
                            var markers = map.option('markers');
                            for(var markerIndex = 0; markerIndex < markers.length; markerIndex++) {{
                            var marker = markers[markerIndex];
                            var markerIcon = markerIcons[markerIndex];
                            marker.iconSrc = markerIcon;
                            }}
                            map.option('markers', markers);                           
            }}";
    }
}
See Also