MKAnnotation,简单示例

MKAnnotation,简单示例

问题描述:

是否有一个简单的示例项目MKAnnotation?我不知道在“addAnnotation”上传递什么,因为它需要一些“id<Annotation>”。 开发人员网站上的例子都是使用数组或解析器等,我只需要一个非常简单的例子来首先了解整个事情的工作原理。MKAnnotation,简单示例

在此先感谢..

编辑:

我成立专班Pin。在.h它写道

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

@interface Pin : NSObject <MKAnnotation> { 
CLLocationCoordinate2D coordinate; 
NSString *title; 
NSString *subTitle; 
} 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readonly) NSString *title; 
@property (nonatomic, readonly) NSString *subTitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description; 
@end 

根据onnoweb回答。

在Pin.m,我实现这样的,根据不同的例子:

#import "Pin.h" 

@implementation Pin 

@synthesize title, subTitle; 

- (CLLocationCoordinate2D) coordinate { 
CLLocationCoordinate2D coords; 
coords.latitude = coordinate.latitude; //i want it that way 
coords.longitude = 7.1352260; //this way it works 

return coords; 

- (NSString *) title { 
return @"Thats the title"; 
} 

- (NSString *) subtitle { 
return @"Thats the sub"; 
} 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description { 

return self; 
} 

- (void) dealloc { 
[super dealloc]; 
} 

@end 

所以,如果我用手工设置的坐标,如指出的评论,它的工作原理。但我希望能够像第一条评论中那样动态设置值。我尝试了各种方法,但都没有解决。因为你没有指定- (CLLocationCoordinate2D) coordinate我试图合成coordinate,但是这样也不起作用。

您能告诉我您的MapPin.m

编辑2:

我设置mapCenter

- (void)viewWillAppear:(BOOL)animated { 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
self.locationManager.delegate = self; 
[self.locationManager startUpdatingLocation]; 

CLLocation *curPos = locationManager.location; 
location = curPos.coordinate; 

CLLocationCoordinate2D mapCenter; 
mapCenter.latitude = location.latitude; 
mapCenter.longitude = location.longitude; 

MKCoordinateSpan mapSpan; 
mapSpan.latitudeDelta = 0.005; 
mapSpan.longitudeDelta = 0.005; 

MKCoordinateRegion mapRegion; 
mapRegion.center = mapCenter; 
mapRegion.span = mapSpan; 

mapKitView.region = mapRegion; 
mapKitView.mapType = MKMapTypeStandard; 
mapKitView.showsUserLocation=TRUE; 
} 

什么不对?

您需要有一个类实现MKAnnotation协议。下面是我的一个项目一个片段:

@interface MapPin : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
} 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readonly) NSString *title; 
@property (nonatomic, readonly) NSString *subtitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description; 

@end 

然后,在你创建的地图,你会叫:

pin = [[MapPin alloc] initWithCoordinates:[track startCoordinates] placeName:@"Start" description:@""]; 
[map addAnnotation:pin]; 

编辑:

这里是实现:

@implementation MapPin 

@synthesize coordinate; 
@synthesize title; 
@synthesize subtitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description { 
    self = [super init]; 
    if (self != nil) { 
     coordinate = location; 
     title = placeName; 
     [title retain]; 
     subtitle = description; 
     [subtitle retain]; 
    } 
    return self; 
} 

- (void)dealloc { 
    [title release]; 
    [subtitle release]; 
    [super dealloc]; 
} 


@end 

希望这会有所帮助, Onno。

+0

谢谢,这已经帮了很多。就像我新的谷歌什么谷歌,并得到它运行,但我不明白。请查看我的更新问题,了解我目前使用的代码。我不知道.m应该是什么样.. – Nareille

+0

请参阅我编辑的答案。 – onnoweb

+0

谢谢,这有助于再次:)一些奇怪行为的最后一个问题:我没有在我的.h中定义类似“坐标”的东西,但Xcode告诉我需要综合它。如果我这样做,我的地图中心向西移动了几百米。如果我忽略了综合,它又重新合理。如何摆脱警告? – Nareille