2015-05-12 46 views
0

我想創建一個使用MapKit的應用程序,我想在地圖上創建註釋,但我很努力地將圖像放在地圖註釋上。將圖像添加到地圖註釋(Swift)

我與編程斯威夫特一個初學者,我已經工作過this tutorial,但我找不到任何地方上網,可以幫助你實現圖像,大多數導遊使用目的C.

這是我的註釋類,這是在我的視圖控制器中調用。

注:我有我的小像我想在我的images.xcassets中顯示文件夾 「mapAnnotation」

let buildingName = location(title: "Building Name", 
          locationName: "Location", 
          coordinate: CLLocationCoordinate2D(latitude: NUMBER, longitude: NUMBER)) 

theMapview.addAnnotation(buildingName) 

這裏是註釋類...

class location: NSObject, MKAnnotation { 
    let title: String 
    let locationName: String 
    let coordinate: CLLocationCoordinate2D 

    //Retrieve an image 
    var image = UIImage(named: "mapAnnotation") 

    init(title: String, locationName:String, coordinate: CLLocationCoordinate2D) { 
     self.title = title 
     self.locationName = locationName 
     self.coordinate = coordinate 
     self.image 
     super.init() 
    } 

    var subtitle: String { 
     return locationName 
    } 
} 
+0

請參閱http://stackoverflow.com/questions/25631410/swift-different-images-for-annotation,http://stackoverflow.com/questions/24467408/swift-add-mkannotationview-to-mkmapview。 – Anna

回答

0

你」重新走上正軌。

MKAnnotation是一個存儲有關地圖註記信息的抽象對象。

現在您需要創建一個MKAnnotationView的自定義子類。這是一個實際的視圖對象,它被添加到註解位置的地圖視圖中。它可以像UIImageView的子類一樣簡單。

然後你需要實現MKMapViewDelegate方法mapView:viewForAnnotation:。該方法將註釋作爲輸入,並返回一個MKAnnotationView。

有關更多信息,請參閱MKAnnoationViewmapView:viewForAnnotation:方法上的Xcode文檔。