2014-09-02 53 views
25

我設法得到一個自定義圖標的註釋在Swift中,但現在我仍然堅持使用2個不同的圖像爲不同的註釋。現在,一個按鈕爲地圖添加註釋。應該有另一個按鈕,也添加了註釋,但與另一個圖標。Swift不同的圖像的註釋

有沒有辦法使用reuseId這個?

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 
} 

回答

74

viewForAnnotation委託方法中,設置在此基礎上annotation的方法中的image被要求。

請務必在之後執行此操作該視圖已出隊或創建(而不僅限於if anView == nil部分)。否則,使用出隊視圖的註釋將顯示之前使用視圖的註釋的圖像。

隨着基本的MKPointAnnotation,一個簡單的方法來區分註釋是由他們的title,但這不是很靈活。

一個更好的辦法是使用實​​現MKAnnotation協議(一種簡單的方法來做到這一點是繼承MKPointAnnotation),並添加任何屬性需要幫助實現自定義邏輯的自定義註釋類。

在自定義類中,添加一個屬性,如imageName,您可以使用該屬性根據註釋自定義圖像。

這個例子子類MKPointAnnotation

class CustomPointAnnotation: MKPointAnnotation { 
    var imageName: String! 
} 

創建CustomPointAnnotation註釋類型,並設置其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" 

viewForAnnotation,使用imageName屬性設置視圖的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

的碼大位。謝謝。 – 2015-03-06 19:55:17

+0

簡單而直接+1 – 2015-03-31 09:15:54

+5

只需要注意一下,您需要實現協議'MKMapViewDelegate'並將您的控制器配置爲func mapView(mapView:MKMapView !,您可以在故事板中或手動編寫代碼) viewForAnnotation註解:MKAnnotation!) - > MKAnnotationView! '工作的方法。這使我絆倒了一下。 – 2015-04-06 17:00:33

11

iOS的Swift代碼與安娜和法比安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

我不得不改變使用:'func mapView(_mapView:MKMapView,viewFor annotation:MKAnnotation) - > MKAnnotationView? {' – Dayna 2017-02-19 23:53:26

+0

真的很棒。它節省了我的時間。 – Raja 2017-11-02 15:39:08