Center View

Is there a way to calculate the coordinates for centering a collection of markers?


1 Reply

VO Vishal Omprasad Syncfusion Team December 30, 2024 09:14 AM UTC

Hi Jason,

Regarding “Is there a way to calculate the coordinates for centering a collection of markers?”:

Your requirement can be achieved by calculating the average Latitude and Longitude of all the markers and setting the resulting coordinates as the MapTileLayer.Center, as shown in the following code snippet.

[C#]:

foreach (var marker in this.markers)

{

    this.totalLatitude = this.totalLatitude + marker.Latitude;

    this.totalLongitude = this.totalLongitude + marker.Longitude;

}

 

this.tileLayer.Center = new MapLatLng(this.totalLatitude/ this.markers.Count, this.totalLongitude / this.markers.Count);

The above approach ensures that the map centers on the area encompassing all the markers.

We have prepared an example sample illustrating the above approach and attached it below for your reference. Please check the sample and let us know whether this meets your requirements.

If the shared information does not meet your needs, kindly provide additional details such as a video or screenshot illustrating the requirement, along with a detailed explanation. Providing additional details will help us validate and address your query more effectively. We appreciate your understanding on this.

Regards,
Vishal O.


Attachment: MapsDemo_104111c4.zip

Loader.
Up arrow icon