2014-09-02 11 views
25

Udało mi się uzyskać niestandardową ikonę pinezki z przypisami w Swift, ale teraz wciąż utknąłem przy użyciu 2 różnych obrazów dla różnych adnotacji. Teraz przycisk dodaje adnotację do mapy. Powinien być inny przycisk, który również dodaje adnotację, ale z inną ikoną.Swift różne obrazy dla Annotation

Czy istnieje sposób na użycie reuseId w tym celu?

class ViewController: UIViewController, MKMapViewDelegate { 

@IBOutlet weak var Map: MKMapView! 

@IBAction func btpressed(sender: AnyObject) { 

    var lat:CLLocationDegrees = 40.748708 
    var long:CLLocationDegrees = -73.985643 
    var latDelta:CLLocationDegrees = 0.01 
    var longDelta:CLLocationDegrees = 0.01 

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta) 
    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long) 
    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span) 

    Map.setRegion(region, animated: true) 


    var information = MKPointAnnotation() 
    information.coordinate = location 
    information.title = "Test Title!" 
    information.subtitle = "Subtitle" 

    Map.addAnnotation(information) 
} 

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if !(annotation is MKPointAnnotation) { 
     return nil 
    } 

    let reuseId = "test" 

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
    if anView == nil { 
     anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     anView.image = UIImage(named:"1.png") 
     anView.canShowCallout = true 
    } 
    else { 
     anView.annotation = annotation 
    } 

    return anView 
} 

Odpowiedz

74

W metodzie viewForAnnotation delegata, ustaw image podstawie której annotation metoda jest wymagana.

Pamiętaj, aby to zrobić po widok został usunięty lub utworzony (i to nie tylko w części if anView == nil). W przeciwnym razie adnotacje, które używają widoku usuniętego z listy, pokaże obraz adnotacji, która poprzednio używała widoku.

Z podstawową wersją MKPointAnnotation jednym z prostych sposobów odróżnienia adnotacji jest ich title, ale nie jest to zbyt elastyczne.

Lepszym rozwiązaniem jest użycie niestandardowej klasy adnotacji, która implementuje protokół MKAnnotation (można to łatwo zrobić na podklasę MKPointAnnotation) i dodać wszelkie właściwości potrzebne do implementacji niestandardowej logiki.

W klasie niestandardowej dodaj właściwość, np. imageName, której można użyć do dostosowania obrazu w oparciu o adnotację.

Ten przykład podklas MKPointAnnotation:

class CustomPointAnnotation: MKPointAnnotation { 
    var imageName: String! 
} 

Tworzenie adnotacji typu CustomPointAnnotation i ustawić ich imageName:

var info1 = CustomPointAnnotation() 
info1.coordinate = CLLocationCoordinate2DMake(42, -84) 
info1.title = "Info1" 
info1.subtitle = "Subtitle" 
info1.imageName = "1.png" 

var info2 = CustomPointAnnotation() 
info2.coordinate = CLLocationCoordinate2DMake(32, -95) 
info2.title = "Info2" 
info2.subtitle = "Subtitle" 
info2.imageName = "2.png" 

W viewForAnnotation, należy użyć właściwości imageName ustawić widok na image:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if !(annotation is CustomPointAnnotation) { 
     return nil 
    } 

    let reuseId = "test" 

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
    if anView == nil { 
     anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     anView.canShowCallout = true 
    } 
    else { 
     anView.annotation = annotation 
    } 

    //Set annotation-specific properties **AFTER** 
    //the view is dequeued or created... 

    let cpa = annotation as CustomPointAnnotation 
    anView.image = UIImage(named:cpa.imageName) 

    return anView 
} 
+0

Wielki kawałek kodu. Dzięki. –

+0

Proste i proste +1 –

+5

Po prostu zwróć uwagę, że musisz zaimplementować protokół "MKMapViewDelegate" i skonfigurować kontroler, aby był twoim delegatem (w scenorysie lub ręcznie w kodzie) dla 'func mapView (mapView: MKMapView !, viewForAnnotation adnotation: MKAnnotation!) -> MKAnnotationView! 'metoda do pracy. To mnie trochę poturbowało. –

11

iOS Kod Swift z pomocą Anna i Fabian Boulegue:

import UIKit 
import MapKit 

class ViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet weak var mapView: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.mapView.delegate = self 

     var info1 = CustomPointAnnotation() 
     info1.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042) 
     info1.title = "Info1" 
     info1.subtitle = "Subtitle" 
     info1.imageName = "flag.png" 

     var info2 = CustomPointAnnotation() 
     info2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098) 
     info2.title = "Info2" 
     info2.subtitle = "Subtitle" 
     info2.imageName = "flag.png" 

     mapView.addAnnotation(info1) 
     mapView.addAnnotation(info2) 
    } 

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

     println("delegate called") 

     if !(annotation is CustomPointAnnotation) { 
      return nil 
     } 

     let reuseId = "test" 

     var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
     if anView == nil { 
      anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      anView.canShowCallout = true 
     } 
     else { 
      anView.annotation = annotation 
     } 

     //Set annotation-specific properties **AFTER** 
     //the view is dequeued or created... 

     let cpa = annotation as CustomPointAnnotation 
     anView.image = UIImage(named:cpa.imageName) 

     return anView 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

class CustomPointAnnotation: MKPointAnnotation { 
    var imageName: String! 
} 
+0

dla Swift 3 Musiałem zmienić, aby użyć: 'func mapView (_ mapView: MKMapView, viewJeśli adnotacja: MKAnnotation) -> MKAnnotationView? {' – Dayna

+0

Naprawdę niesamowite. Oszczędził mój czas. – Raja

Powiązane problemy