2016-12-15 2 views
0

Я реализовал карту и создал простую модель данных для аннотаций.swift, mapView, просмотр аннотации не отображается при нажатии

Булавки показаны на карте, но я все еще не могу получить информацию, нажав на любой контакт.

pins screenshot

Мой код:

import UIKit 
import MapKit 

class MapViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet weak var mapView: MKMapView! 
    @IBOutlet weak var topView: MapDataView! 

    var dummyModel: DummyModel? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     dummyModel = DummyModel() 

     mapView.delegate = self 
     mapView.showsUserLocation = true 
     let region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000, 1000) 
     mapView.setRegion(region, animated: true) 

     let range = 0..<Int((dummyModel?.objectsArray.count)!) 

Массив является действительным, и не имеет ноль:

 for i in range { 
      mapView.addAnnotation((dummyModel?.objectsArray[i])!) 
     } 

Путь я добавить аннотации к карте:

 mapView.selectAnnotation(mapView.annotations[0], animated: true) 
    } 

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     print("viewForAnnotation") 
     let identifier = "PubMapObject" 

     if annotation is PubMapObject { 
      var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 

      if annotationView == nil { 
       annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
       annotationView!.canShowCallout = true 

       let btn = UIButton(type: .detailDisclosure) 
       annotationView!.rightCalloutAccessoryView = btn 
      } else { 
       annotationView!.annotation = annotation 
      } 

      return annotationView 
     } 

     return nil 
    } 

Этот метод никогда не звонил, и я не знаю почему. я думаю, проблема здесь, но я не смог найти):

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
     print("annotationView") 
     let pub = view.annotation as! PubMapObject 
    }   
} 
+0

обновления вашего полный код, так как я думаю, что метод doSelect и другой метод отсутствует в вашем коде –

ответ

1

Попробуйте добавить набор кадра пояснительном перед возвращением пояснительных // моего класса аннотаций и обеспечить ширину и высоту пояснительном перед возвратом вида аннотаций и дать рамку для

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

if (annotation.isKind(of: MKUserLocation.self)) { 
      return nil 
     } 


let reuseId = "PubMapObject" 
     if (annotation.isKind(of: PubMapObject.self)) { 
      let anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      anView.isEnabled = true 
      anView.isUserInteractionEnabled = true 
      let btn = UIButton(type: .detailDisclosure) 
      btn.frame = CGRect(x: 0, y: 0, width: 50, height: 70) 
      anView!.rightCalloutAccessoryView = btn 
      anView.frame = CGRect(x: 0, y: 0, width: 50, height: 70) 
      return anView 
     } 

    return nil 
} 
0

Попробуйте использовать функцию didSelect вместо calloutAccessoryControlTapped.

func mapView(_ mapView: MKMapView, didSelect annotationView: MKAnnotationView) { ... 
+0

Я пробовал этот способ, но метод никогда не называется –

Смежные вопросы