【问题标题】:MKAnnotation, simple exampleMKAnnotation,简单示例
【发布时间】:2011-06-27 15:44:45
【问题描述】:

MKAnnotation 的简单示例项目吗?我不知道在“addAnnotation”要传递什么,因为它想要一些“id<Annotation>”。 开发者网站上的示例都是数组或解析器左右,我只需要一个非常简单的示例来首先了解整个事情是如何工作的。

提前谢谢..

编辑:

我开设了一个班级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;
}

这有什么问题吗?

【问题讨论】:

    标签: iphone objective-c mkannotation


    【解决方案1】:

    您需要有一个实现MKAnnotation 协议的类。这是我的一个项目中的一个 sn-p:

    @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
    

    希望这会有所帮助, 小野。

    【讨论】:

    • 谢谢,这已经很有帮助了。就像那样,我用谷歌搜索了新的东西,并让它运行起来,但我不太明白。请参阅我的更新问题,了解我目前拥有的代码。我不知道 .m 应该是什么样子..
    • 谢谢,这再次帮助了 :) 最后一个关于一些奇怪行为的问题:我没有在我的 .h 中定义“坐标”之类的东西,但 Xcode 告诉我需要合成它。如果这样做,我的 mapCenter 会向西移动数百米。如果我评价合成,它会再次正确居中。如何摆脱警告?
    • 很重要:不要写subTitle,而是subtitle(见标题中非大写的t),否则不会分配你的subtitle!比它工作正常!非常感谢!
    • @Nareille,ios / objective-c 真的 对只要求小写字母很挑剔吗?这看起来很人为的和令人惊讶,如果有更多的细节肯定会很好......
    • @sarnold 好吧,至少它只适用于我的小写字母。如果您查看文档, subtitle 已经被声明为 MKAnnotation 类的属性。我对 iOS 很陌生,但如果我做对了,我的实例只需设置一个 setter 和 getter 来准确访问这个变量。如果情况不同(所以 subTitle),它不会将您的值分配给该属性,并且您的值会在任何地方丢失.. 其他人可能会更感兴趣,但正如我所说,只需从 subTitle 更改为 subtitle 已解决最后一个问题,代码是完美的:)(它之前工作过,但没有设置子)
    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 2013-12-28
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多