Blazor Map Marker complex/nested class contains the Lat and Long

I'm trying to put multiple markers on OSM dynamically, I have a class that contains the Location data.

The Blazor map marker Latitude and Longitude are inside the complex/nested class.

I'm not able to get it's content.

Thanks ahead for the help.


This is the code (The code that documented works):


<SfMaps>

    <MapsZoomSettings ZoomFactor="4" Enable="true"></MapsZoomSettings>

    <MapsCenterPosition Latitude="29.394708" Longitude="-94.954653"></MapsCenterPosition>

    <MapsLayers>

        <MapsLayer UrlTemplate="https://tile.openstreetmap.org/level/tileX/tileY.png" TValue="string">

            @* Add marker *@

            <MapsMarkerSettings>

                <MapsMarker Visible="true" Height="25" Width="15" DataSource="BCities" TValue="Wrapper" LatitudeValuePath="BCity.Latitude"

                LongitudeValuePath="BCity.Longitude" >

                </MapsMarker>

                @*<MapsMarker Visible="true" Height="25" Width="15" DataSource="Cities" TValue="City">

                </MapsMarker>*@

            </MapsMarkerSettings>

            @* Add navigation line *@

            <MapsNavigationLines>

                <MapsNavigationLine Visible="true" Color="blue" Angle="0.1" Latitude="new double[]{34.060620, 40.724546}"

                                    Longitude="new double[]{-118.330491,-73.850344}">

                </MapsNavigationLine>

            </MapsNavigationLines>

        </MapsLayer>

    </MapsLayers>

</SfMaps>


@code{


    public class Wrapper

    {

        public City BCity { get; set; }

    }


    private List<Wrapper> BCities = new List<Wrapper> {

        new Wrapper{BCity = new City { Latitude = 34.060620, Longitude = -118.330491, Name="California" }},

        new Wrapper{BCity = new City{ Latitude = 40.724546, Longitude = -73.850344, Name="New York"}}

    };


    public class City

    {

        public double Latitude { get; set; }

        public double Longitude { get; set; }

        public string Name { get; set; }

    }

    private List<City> Cities = new List<City> {

        new City { Latitude = 34.060620, Longitude = -118.330491, Name="California" },

        new City{ Latitude = 40.724546, Longitude = -73.850344, Name="New York"}

    };

}


1 Reply

IR Indumathi Ravi Syncfusion Team July 21, 2022 01:18 PM UTC

Hi Gabriel,


Thank you for contacting Syncfusion support.


We can achieve the requirement of dynamically adding markers in the Maps component by creating a list with all of the marker settings fields such as data source, visible state, and so on. This list can be used to add markers to the component by iterating over the “MapsMarker” tag. When the list is updated, the markers in the Maps component will be dynamically added. Please find the code snippet for the same below.


Code Snippet:

<Syncfusion.Blazor.Buttons.SfButton id="click" @onclick="AddMarker">Add</Syncfusion.Blazor.Buttons.SfButton>

 

<SfMaps>

    <MapsZoomSettings ZoomFactor="2" Enable="true"></MapsZoomSettings>

    <MapsLayers>

        <MapsLayer UrlTemplate=https://tile.openstreetmap.org/level/tileX/tileY.png TValue="string">

            @* Add marker *@

            <MapsMarkerSettings>

                @foreach (var Marker in MarkersCollection)

                {

                    <MapsMarker Visible="Marker.Visible" DataSource="Marker.DataSource" Height="Marker.Height" Width="Marker.Width" Shape="Marker.Shape" TValue="MarkerCity"

                    ColorValuePath="Color" LatitudeValuePath="Latitude" LongitudeValuePath="Longitude">

                    </MapsMarker>

               }

            </MapsMarkerSettings>

           //..

          //..

        </MapsLayer>

    </MapsLayers>

</SfMaps>

 

@code{

 

    public class Markers

    {

        public bool Visible { get; set; }

        public List<MarkerCity> DataSource { get; set; }

        public double Height { get; set; }

        public double Width { get; set; }

        public MarkerType Shape { get; set; }

    }

 

    public class MarkerCity

    {

        public double Latitude { get; set; }

        public double Longitude { get; set; }

        public string Name { get; set; }

        public string Shape { get; set; }

        public string Color { get; set; }

    };

 

    public List<Markers> MarkersCollection = new List<Markers> {

        new Markers {

            Visible = true,

            Height = 20,

            Shape = MarkerType.Circle,

            Width=20,

            DataSource = new List<MarkerCity>

            {

                new MarkerCity {Latitude=35.145083,Longitude=-117.960260, Name = "California" , Shape="Circle", Color="Green"},

                new MarkerCity { Latitude=40.724546, Longitude=-73.850344 , Name ="NewYork", Shape="Pentagon", Color= "Blue"},

                new MarkerCity {Latitude= 41.657782, Longitude=-91.533857, Name="Iowa", Shape="Rectangle" , Color="Red"}

            },

        },

    };

    void AddMarker()

    {

       MarkersCollection.Add( new Markers {

            Visible = true,

            Height = 20,

            Width=20,

            Shape = MarkerType.Diamond,

            DataSource = new List<MarkerCity>

            {

                new MarkerCity {Latitude=35.6894875,Longitude= 139.6917064, Name = "Tokyo" , Shape="Circle", Color="Pink"},

                new MarkerCity { Latitude=-23.5505199, Longitude=-46.6333094 , Name ="Sao Paulo", Shape="Pentagon", Color= "Violet"},

                new MarkerCity {Latitude= 19.2464696, Longitude= -99.1013498, Name="Mexico City", Shape="Rectangle" , Color="Brown"}

            }

        }); 

    } 

}


We have created a sample and video to demonstrate the same and it can be download from the below link.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/BlazorApp947392249

Video: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DynamicMarker206723601


Please let us know if you need any further assistance.


Regards,

Indumathi R.


Loader.
Up arrow icon