如您所见, 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
}