Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3627306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:52:18+00:00 2026-05-18T23:52:18+00:00

I have a mapView that has annotation added through JSON (feed is stored in

  • 0

I have a mapView that has annotation added through JSON (feed is stored in NSDictionary). Everything works great, but I want to add a feature.

I want the mapView to reload all of the annotations each time the view reappears (every time the tab bar is pressed). T’ve tried putting the part where the JSON is added to the NSDictionary in viewWillAppear {} …. but it does not work.

My code is below. Thanks in advance!

#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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:52:19+00:00Added an answer on May 18, 2026 at 11:52 pm

    I think more details are needed. If viewWillAppear is not getting called then it is probably something to do with the way you are setting up the views.

    These two links should give you some pointers.
    How do I have a view controller run updating code when it is brought to the top of the stack of views?

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have one application it has mapview . problem is that i want to
I have some pins that the user can add. In the callout there is
I have about 400 MKAnnotationView´s that loads simultaneously into the MKMapView. I understand that
I have an application with a SurfaceView and a MapView. They are displayed in
Hi? I am working on a MapView app in Android. I have three markers
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.