调用出视图上方针的位置 Swift ios MKMapView

标签: MKMapView ios Swift
发布时间: 2017/3/6 23:11:48
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 MKAnnotation 与下面的代码中添加一个自定义callout视图︰

func mapView(mapView: MKMapView,
             didSelectAnnotationView view: MKAnnotationView)
{

    if view.annotation is MKUserLocation
    {
        return
    }


    let customAnnotation = view.annotation as! MyAnnotation

    if (applicable) {
        let calloutView = CustomCallout()
        var r : MKMapRect = largeMapView.visibleMapRect
        let p : MKMapPoint = MKMapPointForCoordinate(customAnnotation.coordinate)
        r.origin.x = p.x - r.size.width * 0.5
        r.origin.y = p.y - r.size.height * 0.75

        // 3

        calloutView.translatesAutoresizingMaskIntoConstraints = false
        calloutView.imageView.translatesAutoresizingMaskIntoConstraints = false

        calloutView.clipsToBounds = true
        calloutView.imageView.clipsToBounds = true

        let mapPointCoordinate : CLLocationCoordinate2D = MKCoordinateForMapPoint(p)
        let pointAsCGPoint : CGPoint = self.largeMapView.convertCoordinate(mapPointCoordinate, toPointToView: self.largeMapView)
        calloutView.frame = CGRectMake(pointAsCGPoint.x, pointAsCGPoint.y, viewWidth! * 0.6, (viewHeight! - 110) * 0.6)
        calloutView.addSubview(calloutView.imageView)
        view.addSubview(calloutView)


        view.addConstraint(NSLayoutConstraint(item: calloutView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: (viewHeight! - 110) * 0.6))
        view.addConstraint(NSLayoutConstraint(item: calloutView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: viewWidth! * 0.6))
        calloutView.addConstraint(NSLayoutConstraint(item: calloutView.imageView, attribute: .Top, relatedBy: .Equal, toItem: calloutView, attribute: .Top, multiplier: 1, constant: 0))
        calloutView.addConstraint(NSLayoutConstraint(item: calloutView.imageView, attribute: .Bottom, relatedBy: .Equal, toItem: calloutView, attribute: .Bottom, multiplier: 1, constant: 0))
        calloutView.addConstraint(NSLayoutConstraint(item: calloutView.imageView, attribute: .Leading, relatedBy: .Equal, toItem: calloutView, attribute: .Leading, multiplier: 1, constant: 0))
        calloutView.addConstraint(NSLayoutConstraint(item: calloutView.imageView, attribute: .Trailing, relatedBy: .Equal, toItem: calloutView, attribute: .Trailing, multiplier: 1, constant: 0))


        largeMapView.setVisibleMapRect(r, animated: true)

    }


}

当用户按下针时,我移动地图水平居中对齐和垂直在 25-75 比例按下针。现在我正在尝试中心 CustomCallOut 要为中心正上方的 pin,但无论我做什么,它似乎不规律将自己定位在屏幕上。

解决方法 1:

当使用自动布局时,你不需要去调整 frame 值,也不做你需要关注自己与地图坐标。只是添加在注释视图的约束。

例如,这会添加 60 x 30 正上方的空白灰色callout视图 MKPinAnnotationView :

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    let calloutView = UIView()
    calloutView.translatesAutoresizingMaskIntoConstraints = false
    calloutView.backgroundColor = UIColor.lightGrayColor()
    view.addSubview(calloutView)

    NSLayoutConstraint.activateConstraints([
        calloutView.bottomAnchor.constraintEqualToAnchor(view.topAnchor, constant: 0),
        calloutView.widthAnchor.constraintEqualToConstant(60),
        calloutView.heightAnchor.constraintEqualToConstant(30),
        calloutView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor, constant: view.calloutOffset.x)
    ])
}

所以,设置底部锚点的callout要相对于 MKAnnotationView ,并设置 centerX 抵消 calloutOffset

显然,您可以设置的内容和大小你callout是任何你想要但我希望这说明的方式来获取自定义callout视图注释视图的中心位置。

赞助商