2016-12-15 3 views
0

Ich implementierte Kartenansicht und erstellte einfaches Datenmodell für Anmerkungen.swift, mapView, Anmerkungsansicht wird nicht angezeigt

Die Pins werden auf der Karte angezeigt, aber ich bin immer noch nicht in der Lage, die Details durch Tippen auf einen Pin zu bekommen.

pins screenshot

Mein Code:

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)!) 

Array gültig ist und keine nil:

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

Die Art und Weise i Anmerkung zu einer Karte hinzufügen:

 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 
    } 

Diese Methode nie angerufen und ich weiß nicht warum. Ich denke, das Problem hier ist, aber ich bin nicht in der Lage, es zu finden):

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

Update Ihr vollständiger Code, da ich denke, didSelect-Methode und andere Methode fehlt in Ihrem Code –

Antwort

1

Try Set Rahmen von Anmerkungsansicht hinzuzufügen, bevor Anmerkungsansicht Rückkehr // meine Annotation-Klasse und bieten zu Anmerkungsansicht Breite und Höhe, bevor Sie die Anmerkungsansicht zurückzukehren und geben Rahmen

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

Versuchen didSelect Funktion anstelle von calloutAccessoryControlTapped verwenden.

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

Ich habe diesen Weg versucht, aber die Methode wird nie aufgerufen –

Verwandte Themen