3

有类似名称的问题,例如this one,但这不是我要找的。

基本上我在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()
}
4

2 回答 2

3

所以 MKPolygon 的一个优点是,它的核心实际上是一种 MKMultiPoint,它继承自 MKShape,MKShape 是 MKAnnotation 的子类。只需从 MKPolygon 一路跟随文档。

所以这一切中最好的部分是 MKPolygon 需要符合 MKAnnotation 并因此定义坐标属性。它被定义为注释的中心。

polygonView.coordinate

所以这会给你苹果定义的多边形的中心。现在不幸的是,由于多边形的奇怪形状,它可能不是真正的中心,但应该足够接近。很高兴我们从 Apple 免费获得一些东西。

使用此坐标,您可以创建一个 MKAnnotation 和 MKAnnotationView 来放置在您的叠加层上。

于 2017-05-23T21:10:46.033 回答
1

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.在viewforAnnotation中的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
}
于 2017-07-14T10:26:44.540 回答