在MGLPolygon上添加一个标签

By simon at 2018-02-07 • 0人收藏 • 46人看过

我有一个情况,我需要在地图上绘制一个* MGLPolygon MapBox * ),我也想给一个* _ UILabel像文本在多边形_ 上。标签 必须位于多边形的质心处,并且应始终可见。 我用w找到了一个代码我可以找到一个给定的多边形的质心,但我 无法将标签添加到多边形。我已经在* SWIFT **中完成了编码wift 开发者请帮助我。在此先感谢和快乐编码:)

2 个回复 | 最后更新于 2018-02-07
2018-02-07   #1

如果你有多边形的中心点,你可以用它来创建一个 [MGLPointFeature](https://www.mapbox.com/ios- SDK / API / 3.70.3 /样式%20Primitives.html#/ C:objc \(CS \)MGLPointFeature)。然后 用它创建一个MGLShapeSource和MGLSymbolStyleLayer。提供tex到那个 层。例如:

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)
}
}

2018-02-07   #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,但我想尽快清除这个问题,所以我 使用这个调整,这是确切的吨我想要。

登录后方可回帖

Loading...