|
| 1 | +--- |
| 2 | +title: How to Change Map Markers Colors |
| 3 | +description: Learn how to customize the appearance of map markers by setting and changing their colors in a Blazor application. |
| 4 | +type: how-to |
| 5 | +page_title: How to Change Map Markers Colors |
| 6 | +slug: map-kb-change-marker-colors |
| 7 | +tags: map, markers |
| 8 | +res_type: kb |
| 9 | +ticketid: 1675518 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Map for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +When integrating a [Map for Blazor](slug://components/map/layers), you might want to customize the appearance of your map markers to differentiate them or match your application's theme. This knowledge base article also answers the following questions: |
| 26 | + |
| 27 | +- How can I set the color of a map marker in Blazor? |
| 28 | +- How to change the color of specific map markers based on their titles? |
| 29 | +- How to apply custom styles to map markers in a Blazor application? |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +To customize the color of map markers in a Blazor application, you can [override the default theme styles](slug://themes-override). |
| 34 | + |
| 35 | +### Change the Color to All Map Markers |
| 36 | + |
| 37 | +To change the color for all markers use the following CSS approach: |
| 38 | + |
| 39 | +<div class="skip-repl"></div> |
| 40 | +````RAZOR |
| 41 | +<TelerikMap Class="my-map"> |
| 42 | + <!-- Map configuration --> |
| 43 | +</TelerikMap> |
| 44 | +
|
| 45 | +<style> |
| 46 | + .my-map.k-map .k-marker { |
| 47 | + color: blue; |
| 48 | + } |
| 49 | +</style> |
| 50 | +```` |
| 51 | + |
| 52 | +### Customize Specific Markers |
| 53 | + |
| 54 | +To change the color of specific markers, target them based on their titles using CSS. Check the runnable example below: |
| 55 | + |
| 56 | +````RAZOR |
| 57 | +<TelerikMap Center="@Center" Zoom="3" Class="my-map"> |
| 58 | + <MapLayers> |
| 59 | + <MapLayer Type="@MapLayersType.Marker" |
| 60 | + Data="@MarkerData" |
| 61 | + LocationField="@nameof(MarkerModel.LatLng)" |
| 62 | + TitleField="@nameof(MarkerModel.Title)"> |
| 63 | + </MapLayer> |
| 64 | + <MapLayer Type="@MapLayersType.Tile" |
| 65 | + Attribution="@Attribution" |
| 66 | + Subdomains="@Subdomains" |
| 67 | + UrlTemplate="@UrlTemplate"> |
| 68 | + </MapLayer> |
| 69 | + <MapLayer Type="@MapLayersType.Marker" |
| 70 | + Data="@MarkerData" |
| 71 | + LocationField="@nameof(MarkerModel.LatLng)" |
| 72 | + TitleField="@nameof(MarkerModel.Title)"> |
| 73 | + </MapLayer> |
| 74 | + </MapLayers> |
| 75 | +</TelerikMap> |
| 76 | +
|
| 77 | +<style> |
| 78 | +/* The first line is for the default marker style. The second one is for the hover state. */ |
| 79 | +@foreach (MarkerModel marker in MarkerData) |
| 80 | +{ |
| 81 | + <text> |
| 82 | + .my-map .k-marker[title="@marker.Title"], |
| 83 | + .my-map .k-marker[data-title="@marker.Title"] { |
| 84 | + color: @marker.Color; |
| 85 | + } |
| 86 | + </text> |
| 87 | +} |
| 88 | +</style> |
| 89 | +
|
| 90 | +@code { |
| 91 | + private string[] Subdomains { get; set; } = new string[] { "a", "b", "c" }; |
| 92 | + private string UrlTemplate { get; set; } = "https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png"; |
| 93 | + private string Attribution { get; set; } = "© <a href='https://osm.org/copyright'>OpenStreetMap contributors</a>"; |
| 94 | + private double[] Center { get; set; } = new double[] { 30.268107, -97.744821 }; |
| 95 | +
|
| 96 | + private List<MarkerModel> MarkerData { get; set; } = new List<MarkerModel>() |
| 97 | + { |
| 98 | + new MarkerModel() |
| 99 | + { |
| 100 | + LatLng = new double[] { 30.268107, -97.744821 }, |
| 101 | + Title = "Austin, TX", |
| 102 | + Color = "#008000" |
| 103 | + }, |
| 104 | + new MarkerModel() |
| 105 | + { |
| 106 | + LatLng = new double[] { 37.7749, -122.4194 }, |
| 107 | + Title = "San Francisco, CA", |
| 108 | + Color = "#0000FF" |
| 109 | + } |
| 110 | + }; |
| 111 | +
|
| 112 | + public class MarkerModel |
| 113 | + { |
| 114 | + public double[]? LatLng { get; set; } |
| 115 | + public string Title { get; set; } = null!; |
| 116 | + public string Color { get; set; } = null!; |
| 117 | + } |
| 118 | +} |
| 119 | +```` |
| 120 | + |
| 121 | +## See Also |
| 122 | + |
| 123 | +- [Override Theme Styles](slug://themes-override) |
| 124 | +- [Map - Marker Layer](slug://components/map/layers/marker) |
0 commit comments