Iphone 从JSON数组中提取注释字幕
我试图通过从JSON数组中提取字幕来向MKAnnotation添加字幕。我能够获得标题,并与我的视图控制器协调,如下所示,但我不知道如何从JSON键“cityName”中提取字幕。任何帮助都会很好!谢谢大家! MapViewController.mIphone 从JSON数组中提取注释字幕,iphone,ios,objective-c,mkmapview,mapkit,Iphone,Ios,Objective C,Mkmapview,Mapkit,我试图通过从JSON数组中提取字幕来向MKAnnotation添加字幕。我能够获得标题,并与我的视图控制器协调,如下所示,但我不知道如何从JSON键“cityName”中提取字幕。任何帮助都会很好!谢谢大家! MapViewController.m location.latitude = [dictionary[@"placeLatitude"] doubleValue]; location.longitude = [dictionary[@"placeLongitude"] doubleVal
location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];
newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
andCoordinate:location];
#import "MapViewAnnotation.h"
@implementation MapViewAnnotation
@synthesize title, coordinate, subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
title = ttl;
coordinate = c2d;
subtitle = [SUBTITLE PULLED FROM JSON]
return self;
}
@end
MapViewAnnotation.h
@interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
@end
这里有什么问题?只需完全按照使用
title
属性所做的操作即可 由于可能存在不初始化注释中的每个属性的情况,我建议不要将它们全部放在init方法中
只要这样做,就不需要更改MapViewAnnotation.m或h
location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];
newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
andCoordinate:location];
newAnnotation.subtitle = dictionary[@"cityName"];
这行“-(id)initWithTitle:(NSString*)ttl和坐标:(CLLocationCoordinate2D)c2d”我不能只指定一个ttl和一个c2d变量吗?不,你也可以添加你的
subtitle
属性:-(id)initWithTitle:(NSString*)ttl和坐标:(CLLocationCoordinate2D)c2d和subtitle:(NSString*)subtitle
。在.h
中也要小心更改方法名称谢谢Yaman,这可能是新手,但我也能传递其他字符串吗?假设我想要一个定制的,可以通过隐形的当然你可以。这是一个自定义初始值设定项,因此您可以传递希望在注释上执行附加逻辑的每个参数。好的,谢谢,我可能会提出一个关于如何正确执行此操作的新问题,除非它非常简单,例如以某种方式将其固定到该方法的末尾。多谢!!