Skip to content

Commit 02532a6

Browse files
author
KB Bot
committed
Added new kb article map-center-on-selected-marker
1 parent 4210977 commit 02532a6

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
title: Center the Map on a Selected Marker
3+
description: Learn how to 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-center-on-selected-marker
7+
tags: map, blazor, center, marker, selection, list
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 to the coordinates of the selected marker.
36+
37+
````RAZOR
38+
<TelerikDropDownList Data="@MarkerData"
39+
@bind-Value="@selectedValue"
40+
TextField="Title"
41+
ValueField="Id"
42+
OnChange="@MyOnChangeHandler">
43+
</TelerikDropDownList>
44+
45+
<TelerikMap Center="@MapCenter"
46+
Zoom="3">
47+
<MapLayers>
48+
<MapLayer Type="@MapLayersType.Tile"
49+
Attribution="@LayerAttribution"
50+
Subdomains="@LayerSubdomains"
51+
UrlTemplate="@LayerUrlTemplate">
52+
</MapLayer>
53+
54+
<MapLayer Type="@MapLayersType.Marker"
55+
Data="@MarkerData"
56+
LocationField="@nameof(MarkerModel.LatLng)"
57+
TitleField="@nameof(MarkerModel.Title)">
58+
</MapLayer>
59+
</MapLayers>
60+
</TelerikMap>
61+
62+
@code {
63+
private double[] MapCenter { get; set; } = new double[] { 30.268107, -97.744821 };
64+
private int selectedValue { get; set; }
65+
66+
private readonly string[] LayerSubdomains = new string[] { "a", "b", "c" };
67+
private const string LayerUrlTemplate = "https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png";
68+
private const string LayerAttribution = "&copy; <a href='https://osm.org/copyright'>OpenStreetMap contributors</a>";
69+
70+
private void MyOnChangeHandler(object theUserInput)
71+
{
72+
int selectedId = (int)theUserInput;
73+
MapCenter = MarkerData.First(i => i.Id == selectedId).LatLng;
74+
}
75+
76+
private List<MarkerModel> MarkerData { get; set; } = new List<MarkerModel>() {
77+
new MarkerModel()
78+
{
79+
Id = 0,
80+
LatLng = new double[] { 30.268107, -97.744821 },
81+
Title = "Austin, TX"
82+
},
83+
new MarkerModel()
84+
{
85+
Id = 1,
86+
LatLng = new double[] { 37.7749, -122.4194 },
87+
Title = "San Francisco, CA"
88+
}
89+
};
90+
91+
public class MarkerModel
92+
{
93+
public int Id { get; set; }
94+
public double[]? LatLng { get; set; }
95+
public string Title { get; set; } = string.Empty;
96+
}
97+
}
98+
```
99+
100+
## See Also
101+
102+
- [Map Overview](https://docs.telerik.com/blazor-ui/components/map/overview)
103+
- [Map Markers](https://docs.telerik.com/blazor-ui/components/map/layers/marker)
104+
- [DropDownList Overview](https://docs.telerik.com/blazor-ui/components/dropdownlist/overview)

0 commit comments

Comments
 (0)