-
Notifications
You must be signed in to change notification settings - Fork 80
Added new kb article map-center-on-selected-marker #2583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xristianstefanov
merged 2 commits into
master
from
new-kb-map-center-on-selected-marker-666960cb78bc4d8f874668b863c9f59d
Nov 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| --- | ||
| title: Center the Map on a Selected Marker | ||
| description: Learn how to center a Telerik Map for Blazor component on a marker selected from a list. | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| type: how-to | ||
| page_title: How to Center a Map on Marker in Blazor Applications | ||
| slug: map-center-on-selected-marker | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tags: map, blazor, center, marker, selection, list | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| res_type: kb | ||
| ticketid: 1671734 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>Map for Blazor</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| There's a list of markers. I need to center the map on a marker when it's selected from the list. | ||
|
|
||
| - How can I dynamically update the center of a map based on a marker's coordinates? | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Solution | ||
|
|
||
| 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: | ||
|
|
||
| 1. Use a component like the `TelerikDropDownList` to display the list of markers. Handle the `OnChange` event to update the map's center. | ||
|
|
||
| 2. In the `OnChange` event handler, update the map's `Center` property to the coordinates of the selected marker. | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ````RAZOR | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All comments are addressed except for the two-way binding for Zoom as the Map does not support it. |
||
| <TelerikDropDownList Data="@MarkerData" | ||
| @bind-Value="@selectedValue" | ||
| TextField="Title" | ||
| ValueField="Id" | ||
| OnChange="@MyOnChangeHandler"> | ||
| </TelerikDropDownList> | ||
|
|
||
| <TelerikMap Center="@MapCenter" | ||
| Zoom="3"> | ||
| <MapLayers> | ||
| <MapLayer Type="@MapLayersType.Tile" | ||
| Attribution="@LayerAttribution" | ||
| Subdomains="@LayerSubdomains" | ||
| UrlTemplate="@LayerUrlTemplate"> | ||
| </MapLayer> | ||
|
|
||
| <MapLayer Type="@MapLayersType.Marker" | ||
| Data="@MarkerData" | ||
| LocationField="@nameof(MarkerModel.LatLng)" | ||
| TitleField="@nameof(MarkerModel.Title)"> | ||
| </MapLayer> | ||
| </MapLayers> | ||
| </TelerikMap> | ||
|
|
||
| @code { | ||
| private double[] MapCenter { get; set; } = new double[] { 30.268107, -97.744821 }; | ||
| private int selectedValue { get; set; } | ||
|
|
||
| private readonly string[] LayerSubdomains = new string[] { "a", "b", "c" }; | ||
| private const string LayerUrlTemplate = "https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png"; | ||
| private const string LayerAttribution = "© <a href='https://osm.org/copyright'>OpenStreetMap contributors</a>"; | ||
|
|
||
| private void MyOnChangeHandler(object theUserInput) | ||
| { | ||
| int selectedId = (int)theUserInput; | ||
| MapCenter = MarkerData.First(i => i.Id == selectedId).LatLng; | ||
| } | ||
|
|
||
| private List<MarkerModel> MarkerData { get; set; } = new List<MarkerModel>() { | ||
| new MarkerModel() | ||
| { | ||
| Id = 0, | ||
| LatLng = new double[] { 30.268107, -97.744821 }, | ||
| Title = "Austin, TX" | ||
| }, | ||
| new MarkerModel() | ||
| { | ||
| Id = 1, | ||
| LatLng = new double[] { 37.7749, -122.4194 }, | ||
| Title = "San Francisco, CA" | ||
| } | ||
| }; | ||
|
|
||
| public class MarkerModel | ||
| { | ||
| public int Id { get; set; } | ||
| public double[]? LatLng { get; set; } | ||
| public string Title { get; set; } = string.Empty; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Map Overview](https://docs.telerik.com/blazor-ui/components/map/overview) | ||
| - [Map Markers](https://docs.telerik.com/blazor-ui/components/map/layers/marker) | ||
| - [DropDownList Overview](https://docs.telerik.com/blazor-ui/components/dropdownlist/overview) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.