在MGLPolygon上添加标签

时间:2018-01-22 11:08:35

标签: ios swift mapbox mglmapview

我有一种情况需要在地图上绘制 MGLPolygon MapBox ),我还想提供一个 UILabel文本在多边形上 。标签必须位于多边形的质心,并且应始终可见。我发现了一个代码,我可以找到给定多边形的质心,但我无法在多边形上添加标签。我已经在 SWIFT 中完成了编码,所以快速的开发人员请帮助我。提前致谢和Happy Coding:)

2 个答案:

答案 0 :(得分:2)

func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {

    if let currentAnnotation = annotation as? AreaAnnotation {

        let reuseIdentifier = currentAnnotation.areaTitle
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier!)

        if annotationView == nil {
            annotationView = MGLAnnotationView(reuseIdentifier: reuseIdentifier)
            annotationView?.frame = CGRect(x: 0, y: 0, width: 120, height: 90)
            annotationView!.backgroundColor = UIColor.clear

            let detailsLabel:UILabel = UILabel()
            detailsLabel.frame = CGRect(x: 30, y: 60, width: 60, height: 25)
            detailsLabel.textAlignment = .center
            detailsLabel.text = currentAnnotation.areaTitle
            //                detailsLabel.textColor = UIColor(red:175/255 ,green:255/255, blue:255/255 , alpha:0.75)
            detailsLabel.textColor = UIColor.white
            detailsLabel.font = UIFont(name: "HelveticaNeue-CondensedBlack", size: 15)

            let strokeTextAttributes = [NSAttributedStringKey.strokeColor : UIColor.black, NSAttributedStringKey.strokeWidth : -5.0,] as [NSAttributedStringKey : Any]

            detailsLabel.attributedText = NSAttributedString(string: titleLabel.text!, attributes: strokeTextAttributes)
            detailsLabel.backgroundColor = UIColor.black.withAlphaComponent(1.0)
            detailsLabel.clipsToBounds = true
            detailsLabel.layer.cornerRadius = 5.0
            detailsLabel.layer.borderWidth = 2.0
            detailsLabel.layer.borderColor = UIColor.white.cgColor
            annotationView?.addSubview(detailsLabel)
        }
        return annotationView
    }
    return nil
}

谢谢@jmkiley,但我想尽快清除这个问题,所以我使用了这个调整,这是我想要的。

答案 1 :(得分:0)

如果您有多边形的中心点,则可以使用它来创建MGLPointFeature。然后用它创建MGLShapeSourceMGLSymbolStyleLayer。提供该图层的文本。例如:

import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate {

var mapView : MGLMapView!

var line: MGLPolyline?

override func viewDidLoad() {
    super.viewDidLoad()

    mapView = MGLMapView(frame: view.bounds)
    view.addSubview(mapView)
    mapView.delegate = self

   let coords = [
                  CLLocationCoordinate2D(latitude: 38.0654, longitude:     -88.8135),
                  CLLocationCoordinate2D(latitude: 41.7549, longitude: -88.8135),
                  CLLocationCoordinate2D(latitude: 41.7549, longitude: -83.1226),
                  CLLocationCoordinate2D(latitude: 38.0654, longitude: -83.1226)
    ]

    let polygon = MGLPolygon(coordinates: coords, count: UInt(coords.count))
    mapView.addAnnotation(polygon)
}

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
    let point = MGLPointFeature()
    point.coordinate = CLLocationCoordinate2D(latitude: 40.0781, longitude: -85.6714)

    let source = MGLShapeSource(identifier: "point-source", features: [point], options: nil)
    style.addSource(source)

    let layer = MGLSymbolStyleLayer(identifier: "point-layer", source: source)
    layer.text = MGLStyleValue(rawValue: "Polygon A")
    style.addLayer(layer)
}
}