2010-12-31 6 views
0

JSON(フィードはNSDictionaryに格納されています)によって注釈が追加されたmapViewがあります。すべて素晴らしいですが、私は機能を追加したいです。UIView内のJSONフィードをリロードします

ビューが再表示されるたびに(タブバーが押されるたびに)、mapViewがすべての注釈をリロードするようにします。 T'veは、JSONがNSDictionaryに追加された部分をviewWillAppear {}に入れようとしましたが、動作しません。

私のコードは以下の通りです。前もって感謝します!

#import "MapViewController.h" 
#import "DisplayMap.h" 
#import "JSON/JSON.h" 

@implementation MapViewController 

@synthesize mapView; 
@synthesize selectedType; 
@synthesize locationManager; 


// JSON from Server Actions 
- (NSString *)stringWithUrl:(NSURL *)url { 
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url 
               cachePolicy:NSURLRequestReturnCacheDataElseLoad 
              timeoutInterval:30]; 
    // Fetch the JSON response 
    NSData *urlData; 
    NSURLResponse *response; 
    NSError *error; 

    // Make synchronous request 
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest 
            returningResponse:&response 
               error:&error]; 

    // Construct a String around the Data from the response 
    return [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; 
    } 



- (id)objectWithUrl:(NSURL *)url { 
    SBJsonParser *jsonParser = [SBJsonParser new]; 
    NSString *jsonString = [self stringWithUrl:url]; 

    // Parse the JSON into an Object 
    return [jsonParser objectWithString:jsonString error:NULL]; 
    } 

- (NSDictionary *) downloadFeed { 
    id response = [self objectWithUrl:[NSURL URLWithString:@"http://www.example.com/JSON"]]; 

    NSDictionary *feed = (NSDictionary *)response; 
    return feed; 
    } 




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    locationManager = [[CLLocationManager alloc] init]; 
    [locationManager setDelegate:self]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
    [locationManager startUpdatingLocation]; 

    mapView.mapType = MKMapTypeStandard; 
    mapView.zoomEnabled = YES; 
    mapView.scrollEnabled = YES; 
    mapView.showsUserLocation = YES; 

    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region.span.longitudeDelta = 0.005; 
    region.span.latitudeDelta = 0.005; 
    [mapView setRegion:region animated:YES]; 
    [mapView setDelegate:self]; 


    // Download JSON Feed 
    NSDictionary *feed = [self downloadFeed]; 
    NSArray *streams = (NSArray *)[feed valueForKey:@"stream"]; 

    int Info; 
    for (Info = 0; Info < streams.count; Info++) { 
     NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:Info]; 
     NSLog(@"Time: %@", [stream valueForKey:@"Time"]); 
     NSLog(@"Type: %@", [stream valueForKey:@"Type"]); 
     NSLog(@"Longitude: %@", [stream valueForKey:@"Longitude"]); 
     NSLog(@"Latitude: %@", [stream valueForKey:@"Latitude"]); 

     double lat = [[stream valueForKey:@"Latitude"] doubleValue]; 
     double lon = [[stream valueForKey:@"Longitude"] doubleValue]; 
     NSString *ttype = [[NSString alloc] initWithFormat: @"%@", [stream valueForKey:@"Type"]]; 
     selectedType = ttype; 


     CLLocationCoordinate2D coord = {lat, lon}; 

     DisplayMap *ann = [[DisplayMap alloc] init]; 
     ann.title = [NSString stringWithFormat: @"%@", [stream valueForKey:@"Type"]]; 
     ann.subtitle = [NSString stringWithFormat: @"%@", [stream valueForKey:@"Time"]]; 
     ann.coordinate = coord; 

     [mapView addAnnotation:ann]; 
     } 
     } 
    } 
} 


-(void)viewWillAppear { } 


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    CLLocationCoordinate2D loc = [newLocation coordinate]; 
    [mapView setCenterCoordinate:loc];  
    } 


-(IBAction)refreshMap:(id)sender { 
    // Download JSON Feed 
    NSDictionary *feed = [self downloadFeed]; 
    NSArray *streams = (NSArray *)[feed valueForKey:@"stream"]; 

    int Info; 
    for (Info = 0; Info < streams.count; Info++) { 
     NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:Info]; 
     NSLog(@"Time: %@", [stream valueForKey:@"Time"]); 
     NSLog(@"Type: %@", [stream valueForKey:@"Type"]); 
     NSLog(@"Longitude: %@", [stream valueForKey:@"Longitude"]); 
     NSLog(@"Latitude: %@", [stream valueForKey:@"Latitude"]); 

     double lat = [[stream valueForKey:@"Latitude"] doubleValue]; 
     double lon = [[stream valueForKey:@"Longitude"] doubleValue]; 
     NSString *ttype = [[NSString alloc] initWithFormat: @"%@", [stream valueForKey:@"Type"]]; 
     selectedType = ttype; 


     CLLocationCoordinate2D coord = {lat, lon}; 

     DisplayMap *ann = [[DisplayMap alloc] init]; 
     ann.title = [NSString stringWithFormat: @"%@", [stream valueForKey:@"Type"]]; 
     ann.subtitle = [NSString stringWithFormat: @"%@", [stream valueForKey:@"Time"]]; 
     ann.coordinate = coord; 

     [mapView addAnnotation:ann]; 
    } 
} 


-(MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation { 

    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; //return nil to use default blue dot view 

    static NSString *AnnotationViewID = @"annotationViewID"; 
    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 

    if (annotationView == nil) { 
     annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease]; 
     } 

    annotationView.canShowCallout = YES; 

    if ([annotationView.annotation.title isEqualToString:@"Selected"]) { 
     UIImage *pinImage = [UIImage imageNamed:@"icon_selected.png"]; 
     [annotationView setImage:pinImage]; 
     } 

    annotationView.annotation = annotation; 
    return annotationView; 

    } 


- (void)dealloc { 
    [mapView release]; 

    self.adView.delegate = nil; 
    self.adView = nil; 

    [super dealloc]; 
} 

@end 

答えて

0

さらに詳しい情報が必要だと思います。 viewWillAppearが呼び出されない場合は、おそらくビューを設定する方法と関係します。

これらの2つのリンクは、いくつかの参考になるはずです。 UIViewController.hから How do I have a view controller run updating code when it is brought to the top of the stack of views?

What's the proper way to add a view controller to the view hierarchy?

+0

ありがとうございます!私はこれらを調べます。ちょうどFYI、viewWillAppearは情報をロードします...しかし、2回目または3回目の訪問後にリロードするようには見えません。 –

+0

キャッシュポリシーはどうですか?それが問題だろうか?どのようにNSURLRequestReloadRevalidatingCacheDataに変更するのですか? – Sudhir

+0

美しい!本当にありがとう。 –

2

- (void)viewWillAppear:(BOOL)animated; 

viewWillAppearviewWillAppear:と同じではありません。おそらく、適切な方法をオーバーライドすればうまくいくでしょうか?

関連する問題