我想在自定義引腳上顯示動態標籤。 我使用了MKMapView,CLLocationManager,MKAnnotationView和MKPinAnnotationView。 所以,請幫助我的朋友。我們如何將標籤貼在MapKit的自定義引腳上?
像:
我想在自定義引腳上顯示動態標籤。 我使用了MKMapView,CLLocationManager,MKAnnotationView和MKPinAnnotationView。 所以,請幫助我的朋友。我們如何將標籤貼在MapKit的自定義引腳上?
像:
如果你的意思是你想要的銷對它有一個字母,那麼你需要設置註釋視角的圖像中viewForAnnotation
。如果您打算更改每個註釋的引腳,則需要動態創建圖像。他們將大量的解決這個代碼,但它歸結到這一點
annotationView.image = anImage;
請解釋我如何添加圖像,也標籤在地圖別針。 – 2013-08-26 11:02:06
有很多資源可以解釋如何做這些事情。如果您嘗試了一種方法並且無法使用,請提出一個新問題,說明您做了什麼以及發生了什麼/沒有發生。 – Craig 2013-08-26 22:36:13
首先創建一個自定義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)
}
}
然後確保你的視圖控制器類作爲代表添加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)
你的意思是當針使用者開關,他們看到在特定引腳右邊的名稱或其他文字...? – 2013-02-16 06:14:18
您可以通過在mapkit中設置其名稱和描述以及圖像來使用註釋。 – Dilip 2013-02-16 06:15:17
沒有..當地圖加載時有一些默認引腳,並且它在該引腳上顯示標籤,而不點擊。 [就像:](http://www.ereachconsulting.com/wp-content/uploads/2012/08/map-with-pin.jpg) – 2013-02-16 06:19:04