Skip to content

Commit 2f13055

Browse files
kendo-botKB Botxristianstefanov
authored
Added new kb article map-center-on-selected-marker (#2583)
* Added new kb article map-center-on-selected-marker * kb(Map): address comments --------- Co-authored-by: KB Bot <[email protected]> Co-authored-by: Hristian Stefanov <[email protected]>
1 parent 3601745 commit 2f13055

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: Center Map on Selected Marker
3+
description: Learn how to programmatically center a Telerik Map for Blazor component on a marker selected from a list.
4+
type: how-to
5+
page_title: How to Center a Map on Marker in Blazor Applications
6+
slug: map-kb-center-on-selected-marker
7+
tags: blazor, map, marker
8+
res_type: kb
9+
ticketid: 1671734
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+
There's a list of markers. I need to center the map on a marker when it's selected from the list.
26+
27+
How can I dynamically update the center of a map based on a marker's coordinates?
28+
29+
## Solution
30+
31+
To center the map on a specific marker's coordinates, use a variable to bind the `Center` parameter of the `TelerikMap` component. Update this variable whenever a selection is made from the list. Here's how you can implement this:
32+
33+
1. Use a component like the `TelerikDropDownList` to display the list of markers. Handle the `OnChange` event to update the map's center.
34+
35+
2. In the `OnChange` event handler, update the map's `Center` property value to the coordinates of the selected marker.
36+
37+
>caption Centering the Map on a Selected Marker
38+
39+
````RAZOR
40+
<TelerikDropDownList Data="@MarkerData"
41+
@bind-Value="@SelectedValue"
42+
TextField="@nameof(MarkerModel.Title)"
43+
ValueField="@nameof(MarkerModel.Id)"
44+
OnChange="@MyOnChangeHandler">
45+
</TelerikDropDownList>
46+
47+
<TelerikMap Center="@MapCenter"
48+
Zoom="@MapZoom">
49+
<MapLayers>
50+
<MapLayer Type="@MapLayersType.Tile"
51+
Attribution="@LayerAttribution"
52+
Subdomains="@LayerSubdomains"
53+
UrlTemplate="@LayerUrlTemplate">
54+
</MapLayer>
55+
56+
<MapLayer Type="@MapLayersType.Marker"
57+
Data="@MarkerData"
58+
LocationField="@nameof(MarkerModel.LatLng)"
59+
TitleField="@nameof(MarkerModel.Title)">
60+
</MapLayer>
61+
</MapLayers>
62+
</TelerikMap>
63+
64+
@code {
65+
private double[] MapCenter { get; set; } = new double[] { 30.268107, -97.744821 };
66+
private int SelectedValue { get; set; }
67+
private int MapZoom { get; set; } = 3;
68+
69+
private readonly string[] LayerSubdomains = new string[] { "a", "b", "c" };
70+
private const string LayerUrlTemplate = "https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png";
71+
private const string LayerAttribution = "&copy; <a href='https://osm.org/copyright'>OpenStreetMap contributors</a>";
72+
73+
private void MyOnChangeHandler(object theUserInput)
74+
{
75+
int selectedId = (int)theUserInput;
76+
MapCenter = MarkerData.First(i => i.Id == selectedId).LatLng;
77+
}
78+
79+
private List<MarkerModel> MarkerData { get; set; } = new List<MarkerModel>() {
80+
new MarkerModel()
81+
{
82+
Id = 0,
83+
LatLng = new double[] { 30.268107, -97.744821 },
84+
Title = "Austin, TX"
85+
},
86+
new MarkerModel()
87+
{
88+
Id = 1,
89+
LatLng = new double[] { 37.7749, -122.4194 },
90+
Title = "San Francisco, CA"
91+
}
92+
};
93+
94+
public class MarkerModel
95+
{
96+
public int Id { get; set; }
97+
public double[]? LatLng { get; set; }
98+
public string Title { get; set; } = string.Empty;
99+
}
100+
}
101+
```
102+
103+
## See Also
104+
105+
- [Map Overview](https://docs.telerik.com/blazor-ui/components/map/overview)
106+
- [Map Markers](https://docs.telerik.com/blazor-ui/components/map/layers/marker)
107+
- [DropDownList Overview](https://docs.telerik.com/blazor-ui/components/dropdownlist/overview)

0 commit comments

Comments
 (0)