Спасибо за помощь заранее ... этот убивал меня последние пару часов.
В настоящее время я использую канал 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