2017-02-13 4 views
1

私はさまざまな種類の注釈を作成しようとしています。すべての注釈は美しい理由でカスタマイズする必要があります。MKPointAnnotationで識別子を設定するには

私はviewFor注釈を使用する必要があることを知っていますが、注釈の種類はどのように知ることができますか?あなたがしたいこと何でもプロパティを追加するには

enter image description here

func addZoneAnnotation() { 

    let zoneLocations = ZoneData.fetchZoneLocation(inManageobjectcontext: managedObjectContext!) 

    for zoneLocation in zoneLocations! { 

     let zoneCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: Double(zoneLocation["latitude"]!)!, longitude: Double(zoneLocation["longitude"]!)!) 

     let zoneAnnotation = MKPointAnnotation() 
     zoneAnnotation.coordinate = zoneCoordinate 


     map.addAnnotation(zoneAnnotation) 

    } 

} 
+0

zoneAnnotation.title = "YOURTITLE" –

+0

注釈の種類に応じて、viewForAnnotation Delegateを使用して、タグを設定できます。 –

+1

このリンクを確認する - http://stackoverflow.com/questions/29307173/identify-mkpointannotation-in-mapview –

答えて

0

サブクラスMKPointAnnotation

class MyPointAnnotation : MKPointAnnotation { 
    var identifier: String? 
} 

その後、あなたは次のようにそれを使用することができます。

func addZoneAnnotation() { 
    let zoneLocations = ZoneData.fetchZoneLocation(inManageobjectcontext: managedObjectContext!) 

    for zoneLocation in zoneLocations! { 
     let zoneCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: Double(zoneLocation["latitude"]!)!, longitude: Double(zoneLocation["longitude"]!)!) 
     let zoneAnnotation = MyPointAnnotation() 
     zoneAnnotation.coordinate = zoneCoordinate 
     zoneAnnotation.identifier = "an identifier" 

     map.addAnnotation(zoneAnnotation) 
    } 
} 

そして最後に、あなたがする必要がありますアクセス:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    guard let annotation = annotation as? MyPointAnnotation else { 
     return nil 
    } 

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "reuseIdentifier") 
    if annotationView == nil { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "reuseIdentifier") 
    } else { 
     annotationView?.annotation = annotation 
    } 

    // Now you can identify your point annotation 
    if annotation.identifier == "an identifier" { 
     // do something 
    } 

    return annotationView 
} 
関連する問題