【问题标题】:Add a label in the center of a polygon in MapKit在 MapKit 中的多边形中心添加标签
【发布时间】:2017-05-23 18:14:54
【问题描述】:

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

基本上,我在标准地图上绘制了一堆MKPolygons,给它们一个笔触、一种随机颜色等。我希望能够“命名”它们,添加一个标签或者一个 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()
}

【问题讨论】:

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

标签: ios swift mapkit mkpolygon


【解决方案1】:

所以 MKPolygon 的优点之一是它的核心实际上是一种 MKMultiPoint 类型,它继承自 MKShape,MKShape 是 MKAnnotation 的子类。只需从 MKPolygon 一直遵循文档即可。

所以最好的部分是要求 MKPolygon 符合 MKAnnotation 并因此定义坐标属性。被定义为注解的中心。

polygonView.coordinate

因此,这将为您提供 Apple 定义的多边形中心。现在不幸的是,由于多边形的奇怪形状,它可能不是真正的中心,但应该足够接近。很高兴我们可以从 Apple 免费获得一些东西。

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

【讨论】:

    【解决方案2】:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      • 2021-04-01
      相关资源
      最近更新 更多