private AvailableVehicle[] Clusterize(AvailableVehicle[] vehicles) { // Divide the map in 25 cells (5*5) const int numberOfRows = 5; const int numberOfColumns = 5; // Maximum number of vehicles in a cell before we start displaying a cluster const int cellThreshold = 1; var result = new List <AvailableVehicle>(); var bounds = new Rect(); GetDrawingRect(bounds); var clusterWidth = (bounds.Right - bounds.Left) / numberOfColumns; var clusterHeight = (bounds.Bottom - bounds.Top) / numberOfRows; var list = new List <AvailableVehicle>(vehicles); for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) { for (int colIndex = 0; colIndex < numberOfColumns; colIndex++) { var top = bounds.Top + rowIndex * clusterHeight; var left = bounds.Left + colIndex * clusterWidth; var bottom = bounds.Top + (rowIndex + 1) * clusterHeight; var right = bounds.Left + (colIndex + 1) * clusterWidth; var rect = new Rect(left, top, right, bottom); var vehiclesInRect = list.Where(v => IsVehicleInRect(v, rect)).ToArray(); if (vehiclesInRect.Length > cellThreshold) { var clusterBuilder = new VehicleClusterBuilder(); foreach (var v in vehiclesInRect) { clusterBuilder.Add(v); } result.Add(clusterBuilder.Build()); } else { result.AddRange(vehiclesInRect); } foreach (var v in vehiclesInRect) { list.Remove(v); } } } return(result.ToArray()); }
private AvailableVehicle[] Clusterize(AvailableVehicle[] vehicles) { // Divide the map in 25 cells (5*5) const int numberOfRows = 5; const int numberOfColumns = 5; // Maximum number of vehicles in a cell before we start displaying a cluster const int cellThreshold = 1; var result = new List <AvailableVehicle>(); var bounds = Bounds; var clusterWidth = bounds.Width / numberOfColumns; var clusterHeight = bounds.Height / numberOfRows; var list = new List <AvailableVehicle>(vehicles); for (var rowIndex = 0; rowIndex < numberOfRows; rowIndex++) { for (var colIndex = 0; colIndex < numberOfColumns; colIndex++) { var rect = new CGRect(Bounds.X + colIndex * clusterWidth, Bounds.Y + rowIndex * clusterHeight, clusterWidth, clusterHeight); var vehiclesInRect = list.Where(v => rect.Contains(ConvertCoordinate(new CLLocationCoordinate2D(v.Latitude, v.Longitude), this))).ToArray(); if (vehiclesInRect.Length > cellThreshold) { var clusterBuilder = new VehicleClusterBuilder(); foreach (var v in vehiclesInRect) { clusterBuilder.Add(v); } result.Add(clusterBuilder.Build()); } else { result.AddRange(vehiclesInRect); } foreach (var v in vehiclesInRect) { list.Remove(v); } } } return(result.ToArray()); }