简体   繁体   English

子类化MKAnnotation并将对象添加到MapView

[英]Subclass MKAnnotation and add objects to MapView

I try to subclass the MKAnnotation protocol so I can add my own Store object to the MapView as Annotation 我尝试对MKAnnotation协议进行子类化,以便可以将自己的Store对象添加为MapView作为Annotation

Store.h: 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

Store.m: Store.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 (where I try to add the Store object as Annotation to the MapView: ViewController.m(在此处尝试将Store对象作为注释添加到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];
}

Error: 错误:

-[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'

Question: 题:

  • How can I add the Store object as Annotation to the MapView? 如何将Store对象作为注释添加到MapView?
  • How do I set the title and the subtitle of the annotation? 如何设置注释的标题和副标题?

Your Store class needs a CLLocationCoordinate2D property named coordinate if you plan on not having any custom setters and getters, or setters and getters for that property if you do plan on having custom ones. 您的店铺类需要一个CLLocationCoordinate2D命名属性coordinate如果您计划在没有该属性的任何自定义getter和setter方法,或getter和setter方法,如果你在具有自定义的计划。

Title and subtitle are set similarily, either add properties named title and subtitle and they will be used automatically, or add getters for these two to return whatever text you like. 标题和字幕的设置类似,或者添加名为title和subtitle的属性,它们将被自动使用,或者为这两个属性添加getter以返回您想要的任何文本。

There's more detail here . 这里有更多详细信息

PS Ever since we got ARC, there is no need to synthesize all of your properties, so that line can be ommited. PS自从我们有了ARC之后,就不需要综合您的所有属性,因此可以省略该行。 Additionally, you might want to consider not using iVars, and moving your properties to the .m file in the interface extension, unless you need them to be public. 此外,除非需要公开,否则您可能要考虑不使用iVars,并将属性移动到接口扩展名的.m文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM