【问题标题】:How we can put label on custom pin in MapKit?我们如何在 MapKit 中将标签放在自定义引脚上?
【发布时间】:2013-02-16 06:11:52
【问题描述】:

我想在自定义图钉上显示动态标签。 我使用了 MKMapView、CLLocationManager、MKAnnotationView 和 MKPinAnnotationView。 所以,请朋友们帮助我。

喜欢:

【问题讨论】:

  • 您的意思是,当用户点击 pin 时,他们会在特定 pin 上看到名称或其他文本……?
  • 您可以通过在mapkit中设置其名称和描述和图像来使用注释。
  • No.. 加载地图时有一些默认图钉,它会在该图钉上显示标签,无需点击。 Just like :
  • 那么您可以使用叠加层来实现此目的。
  • 好的..谢谢回复..有任何在线教程或文档可以参考..请建议...

标签: iphone ios6 mkmapview mkannotationview mkpinannotationview


【解决方案1】:

首先创建一个基于MKAnnotationView 的自定义类:

斯威夫特 3

  class CustomAnnotationView: MKAnnotationView {  
    var label: UILabel?

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
      super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
    }
  }

然后确保您的 ViewController 类添加 NKMapViewDelegate 作为委托:

斯威夫特 3

import UIKit
import MapKit

class ViewController: MKMapViewDelegate {
  override func viewDidLoad() {
    super.viewDidLoad()

    mapView.delegate = self
  }
}

然后将以下方法添加到您的ViewController,每当向地图添加注释时,MapView 都会调用该方法:

斯威夫特 3

  func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationIdentifier = "MyCustomAnnotation"
    guard !annotation.isKind(of: MKUserLocation.self) else {
      return nil
    }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
    if annotationView == nil {
      annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
      if case let annotationView as CustomAnnotationView = annotationView {
        annotationView.isEnabled = true
        annotationView.canShowCallout = false
        annotationView.label = UILabel(frame: CGRect(x: -5.5, y: 11.0, width: 22.0, height: 16.5))
        if let label = annotationView.label {
          label.font = UIFont(name: "HelveticaNeue", size: 16.0)
          label.textAlignment = .center
          label.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
          label.adjustsFontSizeToFitWidth = true
          annotationView.addSubview(label)
        }
      }
    }

    if case let annotationView as CustomAnnotationView = annotationView {
      annotationView.annotation = annotation
      annotationView.image = #imageLiteral(resourceName: "YourPinImage")
      if let title = annotation.title,
        let label = annotationView.label {
        label.text = title
      }
    }

    return annotationView
  }

完成所有这些后,可以像这样添加注释:

斯威夫特 3

  let annotation = MKPointAnnotation()
  annotation.coordinate = CLLocationCoordinate2D(latitude: /* latitude */, longitude: /* longitude */)
  annotation.title = "Your Pin Title"
  mapView.addAnnotation(annotation)

【讨论】:

    【解决方案2】:

    如果您的意思是您希望图钉上有一个字母,那么您需要在viewForAnnotation 中设置注释视图的图像。如果您打算更改每个注释的图钉,则需要动态创建图像。他们将为此编写大量代码,但归根结底

    annotationView.image = anImage;
    

    【讨论】:

    • 请解释我如何在地图图钉中添加图像和标签。
    • 有很多资源可以解释如何做这些事情。如果您尝试了一个但它不起作用,请提出一个新问题,说明您做了什么,发生了什么/没有发生什么。
    猜你喜欢
    • 1970-01-01
    • 2021-11-19
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    相关资源
    最近更新 更多