문제

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?

도움이 되었습니까?

해결책

You can use the fitBounds() method. Leaflets reference

다른 팁

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..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top