【问题标题】:Subclass MKAnnotation and add objects to MapView子类化 MKAnnotation 并将对象添加到 MapView
【发布时间】:2014-10-29 13:59:32
【问题描述】:

我尝试将MKAnnotation 协议子类化,以便将我自己的Store 对象添加到MapView 作为注解

Store.h:

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

@interface Store : NSObject <MKAnnotation> {

    NSString *storeType;
    NSString *storeName;
    NSString *storeAddress;
    NSString *storeZip;
    NSString *storeCity;
    NSString *storeLinks;
    NSString *storeBrands;
    UIImage *storePicture;
    NSString *storeDescription;
    NSString *storeLatitude;
    NSString *storeLongitude;
}

@property (nonatomic, retain) NSString *storeType;
@property (nonatomic, retain) NSString *storeName;
@property (nonatomic, retain) NSString *storeAddress;
@property (nonatomic, retain) NSString *storeZip;
@property (nonatomic, retain) NSString *storeCity;
@property (nonatomic, retain) NSString *storeLinks;
@property (nonatomic, retain) NSString *storeBrands;
@property (nonatomic, retain) UIImage *storePicture;
@property (nonatomic, retain) NSString *storeDescription;
@property (nonatomic, retain) NSString *storeLatitude;
@property (nonatomic, retain) NSString *storeLongitude;

@end

商店.m:

#import "Store.h"

@implementation Store

@synthesize storeType, storeName, storeAddress, storeBrands, storeCity, storeLatitude, storeLinks, storeLongitude, storeZip, storePicture, storeDescription;


- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = [self.storeLatitude doubleValue];
    theCoordinate.longitude = [self.storeLongitude doubleValue];
    return theCoordinate;
}

@end

ViewController.m(我尝试将 Store 对象作为 Annotation 添加到 MapView:

for(Store *store in allStores) {
    NSString *location = [store storeAddress];
    location = [location stringByAppendingFormat:@", "];
    location = [location stringByAppendingFormat:[store storeZip]];
    location = [location stringByAppendingFormat:@" "];
    location = [location stringByAppendingFormat:[store storeCity]];

    CLLocationCoordinate2D annotationCoord;
    annotationCoord.latitude = [[store storeLatitude] floatValue];
    annotationCoord.longitude = [[store storeLongitude] floatValue];

    store.coordinate = annotationCoord;

    [self.mapView addAnnotation:store];
}

错误:

-[Store setCoordinate:]: unrecognized selector sent to instance 0x1742a9600
2014-10-29 14:54:01.992 TestMap[10417:2312661] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Store setCoordinate:]: unrecognized selector sent to instance 0x1742a9600'

问题:

  • 如何将 Store 对象作为 Annotation 添加到 MapView?
  • 如何设置注释的标题和副标题?

【问题讨论】:

    标签: ios objective-c iphone xcode mkannotation


    【解决方案1】:

    如果您打算不使用任何自定义 setter 和 getter,则您的 Store 类需要一个名为 coordinateCLLocationCoordinate2D 属性,或者如果您计划使用自定义设置,则该属性需要 setter 和 getter。

    Title 和 subtitle 设置类似,要么添加名为 title 和 subtitle 的属性,它们将被自动使用,要么为这两个添加 getter 以返回您喜欢的任何文本。

    还有更多细节here

    附:自从有了ARC,就不需要综合你所有的属性,所以可以省略那行。此外,您可能需要考虑不使用 iVar,并将您的属性移动到接口扩展中的 .m 文件中,除非您需要将它们公开。

    【讨论】:

    • 感谢 Adis,这正是我需要知道的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 2010-11-19
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多