public override void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); // Default to the region for North America double minLatitude = 37.37 - (28.49 / 2); double maxLatitude = 37.37 + (28.49 / 2); double minLongitide = -96.24 - (31.025 / 2); double maxLongitude = -96.24 + (31.025 / 2); bool initialized = false; foreach (var code in LogBook.GetVisitedAirports()) { Airport airport = Airports.GetAirport(code, AirportCode.FAA); if (airport == null) { continue; } if (!initialized) { minLongitide = maxLongitude = airport.Longitude; minLatitude = maxLatitude = airport.Latitude; initialized = true; } else { minLongitide = Math.Min(minLongitide, airport.Longitude); maxLongitude = Math.Max(maxLongitude, airport.Longitude); minLatitude = Math.Min(minLatitude, airport.Latitude); maxLatitude = Math.Max(maxLatitude, airport.Latitude); } } coordinates = new CLLocationCoordinate2D((minLatitude + maxLatitude) / 2, (minLongitide + maxLongitude) / 2); double spanLongitude = Math.Abs(maxLongitude - minLongitide); double spanLatitude = Math.Abs(maxLatitude - minLatitude); if (initialized) { spanLongitude = Math.Max(spanLongitude, 1.0) * 1.25; spanLatitude = Math.Max(spanLatitude, 1.0) * 1.25; } // Get the region for North America var region = new MKCoordinateRegion( coordinates, new MKCoordinateSpan(spanLatitude, spanLongitude) ); mapView.SetRegion(region, animated); }
IEnumerable <Airport> GetAirports(MKCoordinateRegion region) { if (visited) { foreach (var code in LogBook.GetVisitedAirports()) { Airport airport = Airports.GetAirport(code, AirportCode.FAA); if (airport != null) { yield return(airport); } } } else { foreach (var airport in Airports.GetAirports(region)) { yield return(airport); } } yield break; }