1

出于某种原因,每当我尝试添加注释时,它们都不会自动显示。我已经指出了这个问题。这是因为此块之外的任何内容都不会在我的 ViewDidLoad 中执行。一旦运行此方法,我将如何做到这一点,然后在 ViewDidAppear 或 ViewDidLoad 中显示我的注释?

 CLGeocoder *fgeo = [[CLGeocoder alloc] init];
    [fgeo reverseGeocodeLocation:locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (!error) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            zip = placemark.postalCode;

            NSLog(@"%@", zip);
            // Make the URL connection in order to download JSON/XML data.
            NSString *stringSearch = @"http://api.onebigplanet.com/ws/local-daily-deals?wsToken= &wsVersion=3.0.0&numItems=20&out=json&categoryId=70&radiusInMiles=12&zipCode=";
            NSString *combined = [NSString stringWithFormat:@"%@%@", stringSearch, zip];
            NSLog(@"%@",combined);

            NSURL *url = [NSURL URLWithString:combined];
            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];

            // Error and success message handling.
            [NSURLConnection
             sendAsynchronousRequest:urlRequest
             queue:queue
             completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                 if([data length] > 0 &&
                    error == nil){
                     NSData *jsonData = data;

                     if (jsonData != nil){
                         NSError *error = nil;

                         self.result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

                         if(error == nil)

                             self.mapLocations = result;

                         //NSLog(@"%@", result);

                         for (NSDictionary *location in self.mapLocations[@"merchants"]) {



                                 for (NSDictionary *deals in self.mapLocations[@"deals"]){

                                      for (NSDictionary *cats in deals[@"categories"]){

                                     for (NSDictionary *locations in location[@"branches"]) {
                                         NSLog(@"%@",locations);
                                         mapView.delegate = self;
                                         CLLocationCoordinate2D annotationCoordinate =
                                         CLLocationCoordinate2DMake([locations[@"latitude"] doubleValue],
                                                                    [locations[@"longitude"] doubleValue]);
                                         Annotation *annotation = [[Annotation alloc] init];
                                         annotation.coordinate = annotationCoordinate;
                                         annotation.title = deals[@"title"];
                                         annotation.subtitle = locations[@"street"];
                                         annotation.phoneNumber = locations[@"telephone"];
                                         annotation.description = deals[@"description"];
                                         annotation.value1 = deals[@"value"];
                                         annotation.discountPercent = deals[@"discountPercent"];
                                         annotation.price = deals[@"price"];
                                         annotation.dealy = deals;
                                         annotation.url = deals[@"url"];
                                         annotation.imageUrl = deals[@"imageUrl"];
                                         annotation.iden = cats[@"id"];
                                         [self.mapView addAnnotation:annotation];
                                     }
                                 }
                             }
                         }
                     }
                     else if ([data length] == 0 &&
                              error == nil){
                         NSLog(@"Nothing was downloaded");

                     }
                     else if (error != nil){
                         NSLog(@"Error happened = %@", error);

                     }
                 }

             }];

        }
    }];
4

0 回答 0