简体   繁体   English

从JSON数组中拉出MKAnnotation字幕

[英]Pull MKAnnotation Subtitle from JSON Array

I am trying to add a subtitle to my MKAnnotation by pulling it from a JSON array. 我试图通过从JSON数组中拉出它来为我的MKAnnotation添加一个副标题。 I am able to get the title, and coordinates with my view controller like below, but I can't figure out what to do to get the subtitle to be pulled from the JSON key "cityName". 我能够得到标题,并与我的视图控制器协调如下,但我无法弄清楚如何从JSON键“cityName”中提取字幕。 Any help would be great! 任何帮助都会很棒! Thank you! 谢谢!

MapViewController.m MapViewController.m

location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];  

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
                                               andCoordinate:location];

MapViewAnnotation.h 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

MapViewAnnotation.m MapViewAnnotation.m

#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

What's the problem here ? 这有什么问题? Just do exactly what you're doing with the title property. 只是做你正在做的title属性。

Since there might be situations where you don't initialize every single property your annotation has I'd advise against putting them all into the init method. 由于可能存在您没有初始化注释所具有的每个属性的情况,我建议不要将它们全部放入init方法中。

Just do this and you won't need to change MapViewAnnotation.m or h 只需这样做,您就不需要更改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"];

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

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