我正在尝试创建一个可调整大小的注释
我创建了一个自定义注释
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而不是mkkannotationview这是我不想要的,因为MKOverlay是在地图上绘制的而我想要创建一个notationview,它不是在地图上绘制的
之后,我将有MKZoomScale,我想在MKZoomScale 1到0.25之间调整annotation视图的大小,如果MKZoomScale低于0.25,我想要annotation视图将消失。
谢谢你的帮助!
您可以从mapView的区域var:
的span中获得"缩放"的数量: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会更容易。