2014-07-02 17 views
14

Tôi có một vòng lặp hoạt động để thiết lập chú thích cho tiêu đề và phần tử phụ đề cho một số điểm dữ liệu đang hoạt động. Những gì tôi muốn làm trong cùng một cấu trúc vòng lặp đó là để thiết lập màu pin thành màu tím thay vì mặc định. Những gì tôi không thể tìm ra là những gì tôi cần làm để khai thác vào myMapView để thiết lập pin cho phù hợp.Bị kẹt khi sử dụng MKPinAnnotationView() trong Swift và MapKit

vòng làm việc của tôi và một số nỗ lực một cái gì đó ...

.... 
for var index = 0; index < MySupplierData.count; ++index { 

    // Establish an Annotation 
    myAnnotation = MKPointAnnotation(); 
    ... establish the coordinate,title, subtitle properties - this all works 
    self.theMapView.addAnnotation(myAnnotation) // this works great. 

    // In thinking about PinView and how to set it up I have this... 
    myPinView = MKPinAnnotationView();  
    myPinView.animatesDrop = true; 
    myPinView.pinColor = MKPinAnnotationColor.Purple; 

    // Now how do I get this view to be used for this particular Annotation in theMapView that I am iterating through??? Somehow I need to marry them or know how to replace these attributes directly without the above code for each data point added to the view 
    // It would be nice to have some kind of addPinView. 

} 

Trả lời

35

Bạn cần phải thực hiện các phương pháp viewForAnnotation đại biểu và trả về một MKAnnotationView (hoặc lớp) từ đó.
Điều này cũng giống như trong Mục tiêu-C - SDK cơ bản hoạt động theo cùng một cách.

Xóa việc tạo MKPinAnnotationView khỏi vòng lặp for để thêm chú thích và triển khai phương thức đại biểu thay thế.

Đây là một thực hiện mẫu của phương pháp viewForAnnotation đại biểu trong Swift:

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

    if annotation is MKUserLocation { 
     //return nil so map view draws "blue dot" for standard user location 
     return nil 
    } 

    let reuseId = "pin" 

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView 
    if pinView == nil { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView!.canShowCallout = true 
     pinView!.animatesDrop = true 
     pinView!.pinColor = .Purple 
    } 
    else { 
     pinView!.annotation = annotation 
    } 

    return pinView 
} 
+0

Brilliant - Tks. Tôi bỏ quyền này vào và nó hoạt động. Tôi chỉ cần nhìn thấy một Swift tương đương như tôi không làm rất nhiều Obj C. Một điểm nhỏ trong đó trình biên dịch đã ném một cảnh báo cho việc sử dụng gạch dưới trong dòng: func mapView (_ mapView: MKMapView!, ... Các cảnh báo là "không liên quan" _ "trong paramter 'mapView' không có tên đối số từ khóa – Kokanee

+0

@ Anna, bạn đã có một dự án ví dụ có sẵn? Tôi đập đầu của tôi chống lại bức tường ở đây Cảm ơn – User4

+0

Tôi không có đại diện đủ để bình luận, nhưng các chức năng trên vẫn hoạt động ngoại trừ pinView! .pinColor không được khấu hao. Nó nên đọc pinView! .pinTintColor = UIColor.purpleColor() – Lunarchaos42

Các vấn đề liên quan