自定义当前位置注释引脚未使用核心位置进行更新

时间:2011-03-03 03:08:02

标签: objective-c mapkit core-location mkannotation mkannotationview

尝试为当前位置添加自定义引脚,但该位置不会更新。即使在设置setShowsUserLocation = YES;

之后
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if ([[annotation title] isEqualToString:@"Current Location"]) {
        self.image = [UIImage imageNamed:[NSString stringWithFormat:@"cursor_%i.png", [[Session instance].current_option cursorValue]+1]];
    }

但是,如果我设置为return nil;一切正常,但我丢失了自定义图像。我真的想让它发挥作用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

正如您所见,setShowsUserLocation标志仅使用默认的蓝色气泡显示当前位置。

您需要在此处执行操作,从手机中侦听位置更新,并自行手动重新定位注释。您可以通过创建CLLocationManager实例来执行此操作,并在位置管理器通知其委托更新时删除并替换您的注释:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
  // update annotation position here
}

要重新定位坐标,我有一个符合MKAnnotation协议的类Placemark:

//--- .h ---

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

@interface Placemark : NSObject <MKAnnotation> {
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *strSubtitle;
@property (nonatomic, retain) NSString *strTitle;

-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- (NSString *)subtitle;
- (NSString *)title;

@end

//--- .m ---

@implementation Placemark

@synthesize coordinate;
@synthesize strSubtitle;
@synthesize strTitle;

- (NSString *)subtitle{
    return self.strSubtitle;
}
- (NSString *)title{
    return self.strTitle;
}

-(id)initWithCoordinate:(CLLocationCoordinate2D) c {
    self.coordinate = c;
    [super init];
    return self;
}

@end

然后在我的mapview控制器中,我将注释放在:

- (void) setPlacemarkWithTitle:(NSString *) title andSubtitle:(NSString *) subtitle forLocation: (CLLocationCoordinate2D) location {

    //remove pins already there...
    NSArray *pins = [mapView annotations];

    for (int i = 0; i<[pins count]; i++) {
      [mapView removeAnnotation:[pins objectAtIndex:i]];
    }

    Placemark *placemark=[[Placemark alloc] initWithCoordinate:location];
    placemark.strTitle = title;
    placemark.strSubtitle = subtitle;
    [mapView addAnnotation:placemark];  
    [self setSpan]; //a custom method that ensures the map is centered on the annotation 
}