Ios 在MGLPolygon上添加标签
我需要在地图上绘制一个MGLPolygon(MapBox),并且我还想在多边形上给出一个UILabel-like文本。标签必须位于多边形的质心处,并且始终可见。我找到了一个代码,用它我可以找到给定多边形的质心,但我无法为多边形添加标签。我已经在SWIFT中完成了编码,所以SWIFT开发者请帮助我。提前感谢并愉快地编码:)如果你有多边形的中心点,你可以用它来创建一个多边形。然后用它创建一个Ios 在MGLPolygon上添加标签,ios,swift,mapbox,mglmapview,Ios,Swift,Mapbox,Mglmapview,我需要在地图上绘制一个MGLPolygon(MapBox),并且我还想在多边形上给出一个UILabel-like文本。标签必须位于多边形的质心处,并且始终可见。我找到了一个代码,用它我可以找到给定多边形的质心,但我无法为多边形添加标签。我已经在SWIFT中完成了编码,所以SWIFT开发者请帮助我。提前感谢并愉快地编码:)如果你有多边形的中心点,你可以用它来创建一个多边形。然后用它创建一个MGLShapeSource和MGLSymbolStyleLayer。向该层提供文本。例如: import M
MGLShapeSource
和MGLSymbolStyleLayer
。向该层提供文本。例如:
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)
}
}
谢谢@jmkiley,但我想尽快解决这个问题,所以我使用了这个调整,这正是我想要的。我尝试过你的方法,但问题是标签没有停留在中心,当我缩放(几乎最大缩放)时,MGLSymbolStyleLayer在图层中显示了多个标签,这在我的情况下是不可接受的。休息一下,你的回答很完美。:)谢谢。快乐编码。
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
}