我想获得该区域,以便我可以安装在其中的注释。但是我被徒劳的例外" nsinvalidargumentException",原因:"无效地区"。那么我如何解决这个问题?
var topLeftCoordinate = CLLocationCoordinate2D(latitude: -90, longitude: 180)
var bottomRightCoordinate = CLLocationCoordinate2D(latitude: 90, longitude: -180)
for annotation in mapView.annotations where !annotation.isKind(of: DriverAnnotation.self){
topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, annotation.coordinate.longitude)
topLeftCoordinate.latitude = fmax(topLeftCoordinate.latitude, annotation.coordinate.latitude)
bottomRightCoordinate.longitude = fmin(bottomRightCoordinate.longitude, annotation.coordinate.longitude)
bottomRightCoordinate.latitude = fmax(bottomRightCoordinate.latitude, annotation.coordinate.latitude)
}
var region = mkcoordinateregion(中心: cllocationCoortion2dmake(topleftCoorcory.Latitude- (topleftCoorcorion.latitude -dottomrightcoorceartion.latitude( * 0.5, topleftCoorcorion.longitude (bottomRightCoorceartion.longitude- topleftCoorcorion.longitude( * 0.5(,跨度: mkcoordinatespan(latitudedelta:fabs(topleflftCoorication.Latitude-( bottomRightCoorceartion.Latitude( * 2.0,纵向甲列为: Fabs(底部右坐标。 2.0((
region = mapView.RegionThatFits(region(mapview.setRegion(region, 动画:true(
这是计算符合所有注释范围的矩形的错误方法。
使用此功能,将注释映射到其坐标,然后映射到MKMapRect
实例。reduce/union
功能计算RECT
let coordinates = mapView.annotations.lazy.filter{!($0 is DriverAnnotation)}.map{ $0.coordinate }
let rects = coordinates.map { MKMapRect(origin: MKMapPoint($0), size: MKMapSize()) }
let mapRect = rects.reduce(MKMapRect.null) { $0.union($1) }
mapView.setVisibleMapRect(mapRect, animated: true)
或更简单(感谢苏尔坦(
let annotations = mapView.annotations.filter{!($0 is DriverAnnotation)}
mapView.showAnnotations(annotations, animated: true)