调整大小MKAnnotationView与MKZoomScale - 斯威夫特2.0
问题描述:
我试图做出注解,这将是可调整大小调整大小MKAnnotationView与MKZoomScale - 斯威夫特2.0
我有我创建
class MapAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var image: UIImage? = UIImage(named: "image")
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
}
包括图像的自定义注释和annotationView。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? MapAnnotation {
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier("myCustomAnnotation")
if pin == nil {
pin = MKAnnotationView(annotation: annotation, reuseIdentifier: "myCustomAnnotation")
}
pin!.image = annotation.image
return pin
}
return nil
}
当我调整mapView的大小时,我希望MKAnnotationView也将被调整大小。
我知道MKOverlayRenderer有drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContext context: CGContext)
功能包括zoomScale:MKZoomScale参数。
而且我看到这个解决方案:
let zoomScale: MKZoomScale = mapView.bounds.size.width/
CGFloat(mapView.visibleMapRect.size.width)
但我想在drawMapRect
功能的访问MKZoomScale
,而不是定制的方式。
这意味着我需要制作一个MKOverlayRenderer,而不是MKAnnotationView,我不想这样做,因为MKOverlay正在绘制地图,并且我要制作一个annotationView,它不会绘制在地图上。
之后,我将有MKZoomScale我想调整MKZoomScale 1到0.25之间的annotationViews的大小,如果MKZoomScale低于0.25,我希望annotationViews会消失。
感谢您的帮助!
答
你可以从跨度的“缩放”量上的MapView的区域VAR:
mapView.region.span
然后,在你viewForAnnotation
功能,你可以做一些数学来缩放注释,或有注释做某种功能本身显示较大。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
<pin init methods>
let averageSpan = mapView.region.span.x * mapView.region.span.x * 0.5
let pinScale = someFunctionToReturnScaleBasedOnAverageSpan(averageSpan)
pin.scaleSelf(pinScale)
return pin
}
我不记得了我的头顶,但如果viewForAnnotation
不会被调用时,地图更新其跨度,你需要某种形式的更新功能,消除和添加注释。
它将,然而,更容易使用的MKOverlay。
在函数 'drawMapRect(mapRect:MKMapRect,zoomScale:MKZoomScale,inContext context:CGContext)中,就是那个MKZoomScale' 给你一个我想要使用的默认zoomScale。 如果我想使用MKCoordinateSpan,我已经使用它。 您可以尝试使用功能'drawMapRect'和打印'zoomScale',以及你可以看到我在说什么。 – KillerDeveloper