Ios 在MapKit中的多边形中心添加标签

Ios 在MapKit中的多边形中心添加标签,ios,swift,mapkit,mkpolygon,Ios,Swift,Mapkit,Mkpolygon,也有类似的问题,比如,但这不是我想要的 基本上,我在一张标准地图上画了一堆MKPolygons,给它们一个笔划,一个随机的颜色等等。。我希望能够“命名”它们,添加一个标签,或者可能是一个带有标签的UIView,使它看起来很漂亮。这可能吗 这是我的地图的样子 这里是实现 func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { if overlay is Ma

也有类似的问题,比如,但这不是我想要的

基本上,我在一张标准地图上画了一堆
MKPolygon
s,给它们一个笔划,一个随机的颜色等等。。我希望能够“命名”它们,添加一个标签,或者可能是一个带有标签的UIView,使它看起来很漂亮。这可能吗

这是我的地图的样子

这里是实现

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

    if overlay is MacaronMKPolygon {

        let macaronOverlay = overlay as! MacaronMKPolygon

        let polygonView = MKPolygonRenderer(overlay: macaronOverlay)
        polygonView.strokeColor = UIColor.gray
        polygonView.lineWidth = 1
        polygonView.alpha = shouldShowMacaron ? 1.0 : 0.0
        polygonView.fillColor = macaronOverlay.color
        return polygonView

    }

    return MKOverlayRenderer()
}

因此,MKPolygon的一个优点是,它的核心实际上是一种MKMultiPoint,它继承了MKShape,它是MKAnnotation的一个子类。只需按照文档从MKPolygon往下走

所有这些最好的部分是MKPolygon需要符合MKAnnotation,从而定义坐标的属性。它被定义为注释的中心

polygonView.coordinate
这将给出苹果定义的多边形的中心。不幸的是,对于多边形中的奇怪形状,它可能不是真正的中心,但应该足够近。当我们从苹果免费得到一些东西的时候真是太好了


使用此坐标,您可以创建一个MKAnnotation和MKAnnotationView,以放置在覆盖图上。

1.查找多边形坐标的中心点

 func getCenterCoord(_ LocationPoints: [CLLocationCoordinate2D]) -> CLLocationCoordinate2D{
        var x:Float = 0.0;
        var y:Float = 0.0;
        var z:Float = 0.0;
        for points in LocationPoints {
            let lat = GLKMathDegreesToRadians(Float(points.latitude));
            let long = GLKMathDegreesToRadians(Float(points.longitude));

            x += cos(lat) * cos(long);

            y += cos(lat) * sin(long);

            z += sin(lat);
        }
        x = x / Float(LocationPoints.count);
        y = y / Float(LocationPoints.count);
        z = z / Float(LocationPoints.count);
        let resultLong = atan2(y, x);
        let resultHyp = sqrt(x * x + y * y);
        let resultLat = atan2(z, resultHyp);
        let result = CLLocationCoordinate2D(latitude: CLLocationDegrees(GLKMathRadiansToDegrees(Float(resultLat))), longitude: CLLocationDegrees(GLKMathRadiansToDegrees(Float(resultLong))));
        return result;
    }
2.在注释视图的MKAnnotationView中获取该点的中心位置标签后

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        if (annotation.isKind(of: MKUserLocation.self)) {
            return nil
        }
//annotation class
  if annotation.isKind(of: ZoneNameAnnotationModal.self) {
            let anView = MKAnnotationView(annotation: annotation, reuseIdentifier: "zoneNameInMiddle")
            let ann = annotation as! ZoneNameAnnotationModal
            let height = 30
            let altitudeVw = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: height))
            let lblTitle = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: height))
            lblTitle.font = lblTitle.font.withSize(10)
            lblTitle.text = ann.title
            lblTitle.numberOfLines = 10
            lblTitle.textAlignment = NSTextAlignment.center
            lblTitle.textColor = UIColor.black
            lblTitle.backgroundColor = UIColor.clear
            altitudeVw.layer.cornerRadius = 6.0
            altitudeVw.layer.borderWidth = 1.0
            altitudeVw.layer.borderColor = UIColor.black.cgColor
            altitudeVw.backgroundColor =  UIColor(red: 255.0/255, green: 255.0/255, blue: 255.0/255, alpha: 0.70)
            altitudeVw.addSubview(lblTitle)
            anView.addSubview(altitudeVw)
            return anView
        }
return nil
}

你好你找到这个问题的答案了吗?因为我也在问自己同样的问题,包括MKCircle。谢谢