MKMapView MKPointAnnotation switch event not triggered

I am using MKMapView (delegate set correctly) using MKPointAnnotation. Annotations are generated in this method called the background thread.

func updateMapAnnotations() { for var i = 0; i < DataManager.getStationList().count; i++ { var s = DataManager.getStationList()[i] as Station var annotation = MKPointAnnotation() annotation.setCoordinate(CLLocationCoordinate2D(latitude: s.latitude, longitude: s.longitude)) annotation.title = "\(s.id)" dispatch_async(dispatch_get_main_queue(), { self.mapView.addAnnotation(annotation) }) } } 

Annotations are created here:

 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if (annotation is MKUserLocation) { return nil } let reuseId = "StationAnnotation" let annoStation = DataManager.getStationById(annotation.title!.toInt()!) var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) if anView == nil { anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) let base = UIView(frame: CGRect(x: 0, y: 0, width: 67, height: 26)) let imageView = UIImageView(frame: CGRect(x: 2, y: 2, width: 22, height: 22)) imageView.image = UIImage(named: "test") base.layer.cornerRadius = 3.0 base.clipsToBounds = true base.backgroundColor = UIColor.whiteColor() var priceLabelBig = UILabel(frame: CGRect(x: 25, y: 0, width: 30, height: 25)) priceLabelBig.textColor = UIColor.blackColor() priceLabelBig.font = UIFont(name: priceLabelBig.font.fontName, size: 15) var priceLabelSmall = UILabel(frame: CGRect(x: 55, y: 0, width: 12, height: 15)) priceLabelSmall.textColor = UIColor.blackColor() priceLabelSmall.font = UIFont(name: priceLabelBig.font.fontName, size: 12) if let curPrice = annoStation?.getTextWithSettings().description { var stringLength = countElements(curPrice) var substringToIndex = stringLength - 1 priceLabelBig.text = curPrice.substringToIndex(advance(curPrice.startIndex, substringToIndex)) priceLabelSmall.text = curPrice.substringFromIndex(advance(curPrice.startIndex, substringToIndex)) } base.addSubview(imageView) base.addSubview(priceLabelBig) base.addSubview(priceLabelSmall) anView.addSubview(base) anView.canShowCallout = true } else { anView.annotation = annotation } return anView } 

I know that I need to set the annotation header and set "canShowCallOut" to true in order to make "didSelectAnnotationView" work. As you can see, both are installed correctly.

So, I have a mapView (delegate set), 'canShowCallOut' is true, and the title is set and works.

To go to the details page, I want to track the click on annotations ("didSelectAnnotationView"), but it is not called.

What am I doing wrong?

+3
source share
1 answer

Ok, I found a solution myself.

You must explicitly set the annotation frame. If you just set the subViews for the view, they will be shown, but the view frame will be set to 0, 0 (height, weight). Thus, you simply cannot click on it, because the area is also 0, 0.

My solution is this: interesting line annotationView.frame = CGRect(x: 0, y: 0, width: 67, height: 26) . Everything else is the same. Now clicking on the annotation calls didSelectAnnotationView .

 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if (annotation is MKUserLocation) { return nil } let reuseId = "stationAnnotation" let annoStation = DataManager.getStationById(annotation.title!.toInt()!) var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) if annotationView == nil { annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) annotationView.frame = CGRect(x: 0, y: 0, width: 67, height: 26) let base = UIView(frame: CGRect(x: 0, y: 0, width: 67, height: 26)) base.userInteractionEnabled = true let imageView = UIImageView(frame: CGRect(x: 2, y: 2, width: 22, height: 22)) imageView.image = UIImage(named: "zapfsaeule") base.layer.cornerRadius = 3.0 base.clipsToBounds = true base.backgroundColor = UIColor.whiteColor() var priceLabelBig = UILabel(frame: CGRect(x: 25, y: 0, width: 30, height: 25)) priceLabelBig.textColor = UIColor.blackColor() priceLabelBig.font = UIFont(name: priceLabelBig.font.fontName, size: 15) var priceLabelSmall = UILabel(frame: CGRect(x: 55, y: 0, width: 12, height: 15)) priceLabelSmall.textColor = UIColor.blackColor() priceLabelSmall.font = UIFont(name: priceLabelBig.font.fontName, size: 12) if let curPrice = annoStation?.getPriceWithSettings().description { var stringLength = countElements(curPrice) var substringToIndex = stringLength - 1 priceLabelBig.text = curPrice.substringToIndex(advance(curPrice.startIndex, substringToIndex)) priceLabelSmall.text = curPrice.substringFromIndex(advance(curPrice.startIndex, substringToIndex)) } base.addSubview(imageView) base.addSubview(priceLabelBig) base.addSubview(priceLabelSmall) annotationView.addSubview(base) annotationView.annotation = annotation } else { annotationView.annotation = annotation } return annotationView } 
+5
source

Source: https://habr.com/ru/post/1491854/


All Articles