先谢谢你的帮助......过去几个小时,这个人一直在杀我。
我目前正在使用JSON提要并将其存储在NSDictionary& NSArray的。我正在尝试为每个被拉入的项目添加注释(时间,类型,纬度和经度)。到目前为止,我可以从数组中提取每个值,并在控制台中使用“for”重复它们(请参阅下面的代码)。
如何将这些值存储为注释?任何帮助都会很棒。
以下是我失败的尝试......
- (void)viewDidLoad {
[super viewDidLoad];
// Download JSON Feed
NSDictionary *feed = [self downloadFeed];
NSArray *streams = (NSArray *)[feed valueForKey:@"stream"];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 29.719023;
region.center.longitude = -114.157110;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
int Info;
for (Info = 0; Info < streams.count; Info++) {
NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:Info];
NSLog(@"Time: %@", [stream valueForKey:@"TheTime"]);
NSLog(@"Type: %@", [stream valueForKey:@"Type"]);
NSLog(@"Longitude: %@", [stream valueForKey:@"Longitude"]);
NSLog(@"Latitude: %@", [stream valueForKey:@"Latitude"]);
NSString *getLat = [[NSString alloc] initWithFormat: @"%@", [stream valueForKey:@"Latitude"]];
NSString *getLong = [[NSString alloc] initWithFormat: @"%@", [stream valueForKey:@"Longitude"]];
NSString *getCoord = [[NSString alloc] initWithFormat: @"{%@,%@}", getLat, getLong];
getCoordinates = getCoord;
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"%@", [stream valueForKey:@"TheTime"];
ann.subtitle = @"%@", [stream valueForKey:@"Type"];
ann.coordinate = getCoordinates;
[mapView addAnnotation:ann];
}
}
以下是DisplayMap的代码
DisplayMap.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface DisplayMap : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@end
现在是DisplayMap.m
#import "DisplayMap.h"
@implementation DisplayMap
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
-(void)dealloc{
[title release];
[super dealloc];
}
@end
答案 0 :(得分:1)
getCoordinates是什么类型的?无论如何,它绝对没有被初始化。
假设您将纬度和经度存储为字典中的字符串,这应该可以解决问题。
double lat = [[stream valueForKey:@"Latitude"] doubleValue];
double lon = [[stream valueForKey:@"Longitude"] doubleValue];
CLLocationCoordinate2D coord = { lat, lon };
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = [stream valueForKey:@"TheTime"];
ann.subtitle = [stream valueForKey:@"Type"];
ann.coordinate = coord;
[mapView addAnnotation:ann];
答案 1 :(得分:0)
乍一看:
ann.title = @"%@", [stream valueForKey:@"TheTime"];
应该是
ann.title = [NSString stringWithFormat: @"%@", [stream valueForKey:@"TheTime"]];
我想