2013-03-19 40 views
3

我試圖爲我的地圖上的註釋創建自定義的註釋。我這樣做是通過調整協議MKMapViewDelegate並覆蓋功能mapView:viewForAnnotation:。這一切正常,唯一的問題是我也有showsUserLocation設置爲TRUE,這意味着我在我的mapView:viewForAnnotation:方法中得到的一個「註釋」是類MKUserLocationmkmapview MKUserLocation AnnotationView

我不希望userlocation註釋具有我的自定義annotationview,我希望那個顯示默認的userlocation annotationview!如何返回用戶位置的默認userlocation註釋視圖或將其從註釋中排除(該註釋來自mapView:viewForAnnotation:)?

我試圖捕捉mapView:viewForAnnotation:方法中的UserLocation,但我不知道該返回什麼! (在這個例子中,我返回一個標準MKAnnotationView,但並不像默認的用戶位置註釋顯然)。)

if (![[annotation class] isEqual:[MKUserLocation class]]) { 

     MKAnnotationView *view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"customAnnotation"]; 
     // edit the custom view 
     return view; 
    } 

    MKAnnotationView *view = [[MKAnnotationView alloc] init]; 
    return view; 
+0

請寫出您在圖形頁面已經實現代碼:viewForAnnotation – Pratik 2013-03-19 08:55:29

回答

8

,以示對用戶位置的默認註釋只返回nil對於這種情況下,我就是這麼做的:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    // use your custom annotation 
    if ([annotation isKindOfClass:[MyAnnotationClass class]]) { 
     ... 

     return annotationView; 
    } 

    // use default annotation 
    return nil; 
} 
+0

三次一個正確的答案,但是這是最給點意見;只是返回零的默認註釋。謝謝! – 2013-03-19 09:09:11

2

裏面你viewForAnnotation方法寫這一段代碼。這裏var'map'是你的MKMapview的出口;

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



     //Annoation View for current Location 

     if(map.userLocation != annotation) 

     { 
      UIImage *image = [UIImage imageNamed:@"image.png"]; 
      annotation.image = image; 

      return annotation; 


     } 

     //Annotation View for current location 

     return nil; 

    } 
1

創建自定義AnnotationView

#import <MapKit/MapKit.h> 

@interface AnnotationView : MKPlacemark 

@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate; 

@property (nonatomic, strong) NSString *title; 
@property (nonatomic, strong) NSString *subtitle; 

// you can put here any controllers that you want. (such like UIImage, UIView,...etc) 

@end 

而且在.m file

#import "AnnotationView.h" 

@implementation AnnotationView 

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary 
{ 
    if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary])) 
    { 
     self.coordinate = coordinate; 
    } 
    return self; 
} 

@end 

//使用註釋添加#import "AnnotationView.h"在相關.m file

CLLocationCoordinate2D pCoordinate ; 
pCoordinate.latitude = LatValue; 
pCoordinate.longitude = LanValue; 

// Create Obj Of AnnotationView class 

AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ; 

    annotation.title = @"I m Here"; 
    annotation.subtitle = @"This is Sub Tiitle"; 

[self.mapView addAnnotation:annotation]; 

以上是如何創建AnnotationView的簡單示例。

0

如果標註參數的對象是MKUserLocation類的實例,可以提供一個自定義視圖來表示用戶的位置。要使用默認系統視圖顯示用戶的位置,請返回nil。 如果您未實現此方法,或者如果從用戶位置註釋以外的註釋實現中返回nil,那麼地圖視圖將使用標準引腳註釋視圖。

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

     // if it's the user location, just return nil or custom annotation view. 
     if ([annotation isKindOfClass:[MKUserLocation class]]){ 
      return nil; 
     } else { 
      //return other annotations 
     } 

    }