2

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?

Barry
  • 43
  • 1
  • 5

4 Answers4

3

You can use the fitBounds() method. Leaflets reference

Jérémy
  • 267
  • 2
  • 13
1

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

Yermo Lamers
  • 1,911
  • 14
  • 25
0

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

Dimitry
  • 71
  • 1
  • 6
0

map.fitBounds(markers.getBounds());

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

Kavin Mehta
  • 810
  • 10
  • 18