Question

I have created a mobile map with Cloudmade Leaflet and have it so a number of markers are generated to coincide with LatLong values in a database. However, I can't figure out how to set the initial Map view to set the zoom and centre the map on the group of markers. I presume I need to use setBounds but for this I need to know what the NorthEast and SouthWest coordinates are.

Do I need to go through the whole array of marker coordinates to find the North-most, East-most, South-most and West-mode values or is there an easier way?

Was it helpful?

Solution

You can use the fitBounds() method. Leaflets reference

OTHER TIPS

Create a L.LatLngBounds object and loop through your markers calling L.LatLngBounds.extend() for each lat/lng.

I think the easiest way would be to do something like this:

int minLat = int.MaxValue;
int minLong = int.MaxValue;
int maxLat = int.MinValue;
int maxLong = int.MinValue;

foreach (var point in coordinates) 
{
    minLat = Math.Min(point.Latitude(), minLat);
    minLong = Math.Min(point.Longitude(), minLong);
    maxLat = Math.Max(point.Latitude(), maxLat);
    maxLong = Math.Max(point.Longitude(), maxLong);
} 

This has worked well for me in C#.

map.fitBounds(markers.getBounds());

should work...provided you have same names as the examples..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top