在viewDidAppear上显示用户位置

时间:2011-08-15 11:13:49

标签: iphone annotations mapkit android-mapview

我正在尝试在加载视图时为用户的当前位置添加注释。我该怎么办呢?如果我有新的例子会很好。

万分感谢。

2 个答案:

答案 0 :(得分:1)

在显示当前位置作为注释时,我执行了以下操作:

    // Set up the annotation
LocationAnnotation *annotation = [[LocationAnnotation alloc] initWithLatitude:[numberFormatter numberFromString:[[location valueForKey:@"latitude"] description]] andLongitude:[numberFormatter numberFromString:[[location valueForKey:@"longitude"] description]] andTitle:[location valueForKey:@"name"] andSubtitle:[location valueForKey:@"notes"]];
[numberFormatter release];

[self.mapView addAnnotation:annotation];

Annotation标头类如下所示:

@interface LocationAnnotation : NSObject <MKAnnotation> {

UIImage *image;
NSNumber *latitude;
NSNumber *longitude;
NSString *locationTitle;
NSString *locationSubtitle;
}

@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) NSNumber *latitude;
@property (nonatomic, retain) NSNumber *longitude;

@property(nonatomic,retain) NSString *locationTitle;
@property(nonatomic,retain) NSString *locationSubtitle;

-(id)initWithLatitude:(NSNumber *)aLatitude andLongitude:(NSNumber *)aLongitude andTitle:(NSString *)aLocationTitle andSubtitle:(NSString *)aLocationSubtitle;

@end

您正在寻找addAnnotation方法和MKAnnotation协议。

如果您想要显示自定义位置,则不会像在此处提到的那样创建自己的MKAnnotation

loading custom image on mkmaps

答案 1 :(得分:1)

确保将showsUserLocation的{​​{1}}属性设置为MKMapView

此属性仅启用显示用户位置。