在 Swift 中的 Apple 地图上添加标记标签
Add marker label on Apple Maps in Swift
基于此,可以在 Google 地图上为 iOS 添加标记 label/badge - Add text on custom marker on google map for ios
有谁知道如何为 Apple 地图做这件事?我需要这样的东西:
您可以尝试将 UILabel
放在图像上:
func mapView(_ mapView: MKMapView,
viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Leave default annotation for user location
if annotation is MKUserLocation {
return nil
}
let reuseID = "Location"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseID)
if annotationView == nil {
let pin = MKAnnotationView(annotation: annotation,
reuseIdentifier: reuseID)
pin.image = UIImage(named: "cabifyPin")
pin.isEnabled = true
pin.canShowCallout = true
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
label.textColor = .white
label.text = annotation.id // set text here
pin.addSubview(label)
annotationView = pin
} else {
annotationView?.annotation = annotation
}
return annotationView
}
基于此,可以在 Google 地图上为 iOS 添加标记 label/badge - Add text on custom marker on google map for ios
有谁知道如何为 Apple 地图做这件事?我需要这样的东西:
您可以尝试将 UILabel
放在图像上:
func mapView(_ mapView: MKMapView,
viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Leave default annotation for user location
if annotation is MKUserLocation {
return nil
}
let reuseID = "Location"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseID)
if annotationView == nil {
let pin = MKAnnotationView(annotation: annotation,
reuseIdentifier: reuseID)
pin.image = UIImage(named: "cabifyPin")
pin.isEnabled = true
pin.canShowCallout = true
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
label.textColor = .white
label.text = annotation.id // set text here
pin.addSubview(label)
annotationView = pin
} else {
annotationView?.annotation = annotation
}
return annotationView
}