Completed
Last Updated: 28 Nov 2024 14:38 by ADMIN
Phil
Created on: 30 Jan 2023 21:46
Category: Map
Type: Feature Request
4
Add ability to set map centre at runtime
When trying to set center at runtime, warnings are generated explaining that center should only be set from within the component. It would be hugly beneficial to be able to set centre dynamically at runtime to manipulate the map and have it pan programmatically to different locations.
2 comments
ADMIN
Hristian Stefanov
Posted on: 28 Nov 2024 14:38

Hi everyone,

I'm changing the status of this item to "Completed" as setting the Map center at runtime is possible. Here is an example:

<TelerikDropDownList Data="@MarkerData" TextField="Title" ValueField="Id" @bind-Value="selectedValue" 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 = "&copy; <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;
    }
}

Regards,
Hristian Stefanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Alexander
Posted on: 03 Oct 2023 22:18

You just need to bind it to a variable like:

 

<TelerikMap Center="@Center" />

 

@code

{

     double Center = new double[] { 0.0, 0.0 };

     void Change(double lat, double lon)

     {

          Center = new double[] { lat, lon }

     }

}