1

尝试为当前位置添加自定义图钉,但位置未更新。即使设置后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;一切正常,但我会丢失自定义图像。我真的很想让这个工作。任何帮助将不胜感激。

4

1 回答 1

1

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

您需要在此处执行的操作是从手机监听位置更新并自己手动重新定位您的注释。您可以通过创建 CLLocationManager 实例并在位置管理器通知其代表更新时删除和替换您的注释来做到这一点:

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

为了重新定位坐标,我有一个类 Placemark,它符合 MKAnnotation 协议:

//--- .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

然后在我的地图视图控制器中,我将注释放置在:

- (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 
} 
于 2011-03-03T03:30:13.513 回答