2016-12-30 60 views
0

我试过以下不同的教程,以使我的MKAnnotations有一个附件项目,当点击时会延伸到不同的页面;但是,我无法展示该项目。这里是我的代码已经在研究不同来源后,拿出了:MKAnnotationView现在显示AccessoryItem

extension MapController : MKMapViewDelegate { 

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

    let identifier = "PlaceAnnotation" 

    if annotation is PlaceAnnotation { 

     var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 

     if annotationView == nil { 

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

     } else { 

      annotationView!.annotation = annotation 
     } 
     annotationView!.leftCalloutAccessoryView = nil 
     annotationView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure) 
     return annotationView 
    } 

    return nil 
} 

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    let place = view.annotation as! PlaceAnnotation 
    let placeName = place.title 
    print(placeName!) 

    let placeInfo = place.placeObject 

    let ac = UIAlertController(title: placeInfo?.title, message: placeInfo?.description, preferredStyle: .alert) 
    ac.addAction(UIAlertAction(title: "OK", style: .default)) 
    present(ac, animated: true) 

} 

} 

然后,要创建注释,我有这样的:

for place in places { 

     let placeCoord = CLLocationCoordinate2D(latitude: CLLocationDegrees(place.latitude)!, longitude: CLLocationDegrees(place.longitude)!) 

     let annotation = PlaceAnnotation(title: place.title, coordinate: placeCoord, placeObject: place) 
     mapView.addAnnotation(annotation) 

    } 

而且PlaceAnnotation类如下:

class PlaceAnnotation: NSObject, MKAnnotation { 

var coordinate: CLLocationCoordinate2D 
var title: String? 
var placeObject: Location? 

init(title: String, coordinate: CLLocationCoordinate2D, placeObject: Location) { 
    self.title = title 
    self.coordinate = coordinate 
    self.placeObject = placeObject 
} 
} 

单击时显示在注释上的唯一内容是标题,但没有别的。感谢您的帮助,非常感谢!

(我在斯威夫特3个工作)

+0

显示你的地图视图创建代码 – Matt

回答

0

我想你的代码,它完美对我很好。

但是当我删除说,

mapView.delegate = self 

配件项目没有露面,因为委托不叫。

+0

有多尴尬......我忘了写mapView.delegate piece哈哈。非常感谢! – JD2017

+0

不客气。 – Matt