2013-03-12 58 views
2

認爲使用我自己的自定義圖釘進行註釋會非常容易。iOS:爲什麼我無法使用mapkit顯示自定義註釋引腳圖像?

但我從來沒有能夠得到它的工作,我不知道爲什麼!

我只是用:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    if([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    NSString *annotationIdentifier = @"CustomViewAnnotation"; 
    CustomAnnotationView * customAnnotationView = (CustomAnnotationView *) [self.mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; 
    if(!customAnnotationView) 
    { 
     customAnnotationView=[[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation 
                     reuseIdentifier:annotationIdentifier 
                    annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]]; 
    } 

    customAnnotationView.image=[UIImage imageNamed:@"map_location_pin.png"]; 

    customAnnotationView.canShowCallout= YES; 

    return customAnnotationView; 
} 

我認爲這應該工作,作爲一個UIImage是它想要什​​麼,我有我的項目中.png文件。

它從來沒有使用我的形象,但只是標準的紅色別針。我在這裏做錯了什麼?

+0

請參閱:http://www.raywenderlich.com/2847/introduction-to-mapkit-on-ios-tutorial – Mrunal 2013-03-12 13:08:10

回答

6

一對夫婦的想法:

  1. 你定義delegateMKMapView?你有沒有在這裏放置一個斷點或NSLog以確保你的viewForAnnotation被調用?

  2. 你還沒有分享你的CustomAnnotationView接口/實現細節,但你不需要子類MKAnnotationView,除非你在那裏做一些特殊的事情。如果你剛剛更換的圖像,你可以做:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
    { 
        if([annotation isKindOfClass:[MKUserLocation class]]) { 
         return nil; 
        } 
    
        NSString *annotationIdentifier = @"CustomViewAnnotation"; 
        MKAnnotationView* annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; 
    
        if (annotationView) { 
         annotationView.annotation = annotation; 
        } else { 
         annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; 
        } 
    
        annotationView.image = [UIImage imageNamed:@"map_location_pin.png"]; 
        annotationView.canShowCallout= YES; 
    
        return annotationView; 
    } 
    

    很明顯,不過,如果你(當引腳被拖動或下降,如自定義註釋),然後繼續前進,必須做一些有趣的事情您的CustomAnnotationView子類MKAnnotationView。這取決於你想要做什麼。

  3. 無關的你原來的問題,我建議:

    • viewForAnnotation方法只使用本地mapView變量,而不是引用類屬性,self.mapView(雖然他們無疑是相同的) ,

    • 您考慮重新命名您的自定義init方法。因此,而不是:

      customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation 
                         reuseIdentifier:annotationIdentifier 
                        annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]]; 
      

      ,你會做這樣的事情:如果你仍然有麻煩

      customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation 
                       reuseIdentifier:annotationIdentifier 
                          image:[UIImage imageNamed:@"map_location_pin.png"]]; 
      

,與我們分享您的一些CustomAnnotationView代碼。

2

我認爲這是支持you.can你試試這個code.and你可以加我的註解h和.m文件,並做一些改變了我annotataion

我annotation.h

#import <Foundation/Foundation.h> 
    #import <MapKit/MapKit.h> 

    @interface MyAnnotation : NSObject<MKAnnotation> { 

     CLLocationCoordinate2D coordinate; 
     NSString*    title; 
     NSString*    subtitle; 
     UIImage*    image; 
    } 

    @property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
    @property (nonatomic, copy)  NSString*    title; 
    @property (nonatomic, copy)  NSString*    subtitle; 
    @property (nonatomic, retain)  UIImage*    image; 

    @end 


my annotation.m 


     #import "MyAnnotation.h" 


    @implementation MyAnnotation 

    @synthesize title; 
    @synthesize subtitle; 
    @synthesize coordinate; 
    @synthesize image; 
    - (void)dealloc 
    { 
     [super dealloc]; 
     self.title = nil; 
     self.subtitle = nil; 
     self.image=nil; 
    } 
    @end 





     - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 

    { 

     //NSLog(@"welcome into the map view annotation"); 

     // if it's the user location, just return nil. 

     if ([annotation isKindOfClass:[MKUserLocation class]]) 

      return nil; 

     // try to dequeue an existing pin view first 
     static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 

     MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]autorelease]; 

     //annotation.image = [UIImage imageNamed:@"shop-1.png"]; 

     //[pinView setImage:[UIImage imageNamed:@"shop-1.png"]]; 

     UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:((MyAnnotation *)annotation).image]; 
     CGRect newBounds = CGRectMake(0.0, 0.0, 32.0, 32.0); 
     [thumbnailImageView setBounds:newBounds]; 
     pinView.leftCalloutAccessoryView = thumbnailImageView; 
     [thumbnailImageView release]; 

     //UIImageView *profileIconView = [[UIImageView alloc] initWithImage:featuredDealcouponImage1]; 
     // [pinView setImage:featuredDealcouponImage1]; 
     pinView.animatesDrop=NO; 

     pinView.canShowCallout=YES; 

     pinView.pinColor=MKPinAnnotationColorGreen; 

     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

     [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 

     [rightButton addTarget:self action:@selector(_annotation_btn_click:) forControlEvents:UIControlEventTouchUpInside]; 

     pinView.rightCalloutAccessoryView = rightButton; 




     return pinView; 

    } 
0

確保你繼承

NSObject <MKAnnotation> 

,而不是

MKPinAnnotationView