Skip to content

Commit f89acd3

Browse files
author
KB Bot
committed
Added new kb article map-kb-customize-marker-colors
1 parent a3b02d6 commit f89acd3

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
title: How to Customize Map Marker Colors in Blazor Applications
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 Customize Map Marker Colors in Blazor Applications
6+
slug: map-kb-customize-marker-colors
7+
tags: map, markers
8+
res_type: kb
9+
ticketid: 1675518
10+
---
11+
12+
## Description
13+
14+
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:
15+
16+
- How can I set the color of a map marker in Blazor?
17+
- How to change the color of specific map markers based on their titles?
18+
- How to apply custom styles to map markers in a Blazor application?
19+
20+
## Environment
21+
22+
<table>
23+
<tbody>
24+
<tr>
25+
<td>Product</td>
26+
<td>Map for Blazor</td>
27+
</tr>
28+
</tbody>
29+
</table>
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+
### Customizing 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="@MarkerData1"
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="@MarkerData2"
71+
LocationField="@nameof(MarkerModel.LatLng)"
72+
TitleField="@nameof(MarkerModel.Title)">
73+
</MapLayer>
74+
</MapLayers>
75+
</TelerikMap>
76+
77+
<style>
78+
.my-map .k-marker[title="San Francisco, CA"] {
79+
color: blue;
80+
}
81+
82+
.my-map .k-marker[title="Austin, TX"] {
83+
color: green;
84+
}
85+
</style>
86+
87+
@code {
88+
private string[] Subdomains { get; set; } = new string[] { "a", "b", "c" };
89+
private string UrlTemplate { get; set; } = "https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png";
90+
private string Attribution { get; set; } = "&copy; <a href='https://osm.org/copyright'>OpenStreetMap contributors</a>";
91+
private double[] Center { get; set; } = new double[] { 30.268107, -97.744821 };
92+
93+
private List<MarkerModel> MarkerData1 { get; set; } = new List<MarkerModel>()
94+
{
95+
new MarkerModel()
96+
{
97+
LatLng = new double[] { 30.268107, -97.744821 },
98+
Title = "Austin, TX"
99+
}
100+
};
101+
102+
private List<MarkerModel> MarkerData2 { get; set; } = new List<MarkerModel>()
103+
{
104+
new MarkerModel()
105+
{
106+
LatLng = new double[] { 37.7749, -122.4194 },
107+
Title = "San Francisco, CA"
108+
}
109+
};
110+
111+
public class MarkerModel
112+
{
113+
public double[] LatLng { get; set; }
114+
public string Title { get; set; } = null!;
115+
}
116+
}
117+
````
118+
119+
## See Also
120+
121+
- [Override Theme Styles](slug://themes-override)
122+
- [Map - Marker Layer](slug://components/map/layers/marker)

0 commit comments

Comments
 (0)