开发者

Place Annotations on Map with NSDictionary?

Thanks for the help in advance... this one has been killing me for the past couple of hours.

I am currently pulling in a JSON feed and storing it in a NSDictionary & NSArray. I'm trying to add an annotation for each item being pulled in (time, type, latitude, and longitude). So far, I can extract each value from the Array and have them all repeat with a "for" in the console (see code below).

How to I store these values as an annotation? Any help would be great.

Below is my failed attempt...


- (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];

  }
}

Here is the code for 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

And now DisplayMap.m

#import "DisplayMap.h"

@implementation DisplayMap

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;


-(void)dealloc{
    [title release];
    [super dealloc];
    }

@end


What type is getCoordinates? Whatever the case, its definitely not being initialized right.

Assuming that your storing Latitude and Longitude as strings in your dictionary, this should do the trick.

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];


At first glance :

  ann.title = @"%@", [stream valueForKey:@"TheTime"];

should be

  ann.title = [NSString stringWithFormat: @"%@", [stream valueForKey:@"TheTime"]];

I guess

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜