@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
map.delegate = self
showAlarms()
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func showAlarms(){
map.region.center.latitude = 10.733051
map.region.center.longitude = 76.763042
map.region.span.latitudeDelta = 1
map.region.span.longitudeDelta = 1
}
func mapView(mapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
//pinView!.animatesDrop = true
pinView!.image = UIImage(named:"annotation")!
}
else {
pinView!.annotation = annotation
}
return pinView
}
我需要在swift ios中为我的mapview显示自定义图片图像。自定义图像既不加载引脚也不加载。我有一个名为“annotation”的图像,我需要在mapview中加载。
答案 0 :(得分:6)
mapview.delegate = self
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
return nil
}
let annotationIdentifier = "Identifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
if let annotationView = annotationView {
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImagename”)
}
return annotationView
}
答案 1 :(得分:0)
您还可以创建自定义MKAnnotationView并在其中设置图像:
{
"parameter" :
[ {
"key" : "paymentType",
"value" : "F"
} ]
}
然后你需要做的就是:
class myCustomAnnotationView : MKAnnotationView {
override var annotation: MKAnnotation? {
willSet {
guard let annotation = newValue else {return}
switch annotation {
case is myCustomMKAnnotation:
self.canShowCallout = true
self.image = #yourimage
self.centerOffset = CGPoint(x: 0, y: -self.image!.size.height / 2)
break
default:
return
}
self.setAnimationLayer()
}
}
我觉得它看起来更干净