2014-10-17 46 views
0

我試圖從位置數據庫創建許多針腳,每個針腳都帶有自定義圖像。圖像以base64格式從服務器傳遞,我可以從base64數據創建一個UIImage,但我無法將圖像作爲圖標上傳,而不是每個註釋的圖釘。MKMapViewAnnotation來自自定義圖像的圖像針對多個針腳不同

任何想法,我可以做到這一點?

我試圖按照這些所謂職位,以幫助,但他們只能創建一個預設的一個,我無法弄清楚如何編輯個個所以他們每個人都有一個獨特的圖標:

SO link 1SO link 2

有沒有想法?

這裏是我的代碼來創建針/註釋櫃面有人有興趣:

func mapTheseCoordinates(lat: Double, lng: Double, username: NSString, fullname: NSString) { 
    let location = CLLocationCoordinate2D(
     latitude: lat, 
     longitude: lng 
    ) 
    // 2 
    let span = MKCoordinateSpanMake(0.15, 0.15) 
    let region = MKCoordinateRegion(center: location, span: span) 
    mapView.setRegion(region, animated: true) 

    //3 
    let annotation = MKPointAnnotation() 
    annotation.setCoordinate(location) 
    annotation.title = username 
    annotation.subtitle = fullname 
    mapView.addAnnotation(annotation) 
} 
+0

請參閱http://stackoverflow.com/questions/25631410/swift-different-images-for-annotation。 – Anna 2014-10-17 11:31:37

+0

@Anna當我應用你的代碼時,引腳沒有顯示出來? – Haring10 2014-10-17 11:44:24

回答

0

可以使用的MKMapView的這個代表事件,在這裏你可以改變不同的圖像,按您的註釋

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"]; 
    //MyPin.pinColor = MKPinAnnotationColorPurple; 
    MyPin.highlighted = NO; 
    MyPin.image = [UIImage imageNamed:@"pin_red"]; 

    return MyPin; 
} 

此代表事件將在每次添加註釋時調用,但不要忘記爲mapview設置代理人

[mapLocation addAnnotation:annotObj]; 
+0

我該如何迅速做到這一點?我確實將帖子標記爲swift,並提供了swift代碼,我不太瞭解Objective C。 – Haring10 2014-10-17 11:32:23

+0

我不知道迅速,但我想它應該工作http://stackoverflow.com/questions/25631410/swift-different-images-for-annotation – 2014-10-17 11:40:06