课程布局

时间:2017-01-06 02:10:03

标签: ios swift annotations mkmapview customization

我对两个代码块之间的链接存在问题,希望能够更改注释图像。我是swift的新手,所以我很难在mapView上找到它。我在函数中给出了一个代码,用于在运行时打印字符串,但字符串没有显示在NSLog中。这是否意味着,该功能根本没有运行?我会感谢任何关于我出错的建议。谢谢

class ViewController2: UIViewController {

@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()

//Location for first Pin
let locationOne = CLLocationCoordinate2DMake(-47.016945, 167.852095)

//Location for map centering
let locationNZ = CLLocationCoordinate2DMake(-43.937462, 170.507813)
let span = MKCoordinateSpanMake(9, 9)
let region = MKCoordinateRegion(center: locationNZ, span: span)
mapView.setRegion(region, animated: true)

//Create annotation one
let annotation = MKPointAnnotation()
annotation.coordinate = locationOne
annotation.subtitle = "Park"
annotation.title = "Rakiura National Park"

//Add annotation to the map
mapView.addAnnotation(annotation)}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()}




func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

print("func")
guard !annotation.isKind(of: MKUserLocation.self) else {
return nil}

let annotationIdentifier = "AnnotationIdentifier"

var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation}
 else {

let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView = av}

if let annotationView = annotationView {
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "mapIcon.png")}

return annotationView}}

1 个答案:

答案 0 :(得分:0)

听起来你可能有我忘了设置委托,所以我的委托从来没有被称为问题;当你的委托方法没有触发时,这应该始终是你怀疑的。试试

mapView.delegate = self