Obj-C-不是所有的自定义MKMapView注释都始终显示在MapView上?

问题描述

我正在尝试使用以下代码(餐厅,公园,聚会场所和商店)在我的mapView上显示多个位置数组。当我打开应用程序时,并不是所有数组内的地址都显示在我的mapView上(即使返回了所有数据)-例如有时,只有“商店”和“公园”注释数组会显示在我的地图上,但是看不到饭店(即使成功返回了所有数组的数据)。如果我关闭并重新打开该应用程序,有时公园会显示在地图上,但没有其他显示。知道为什么会发生这种情况以及如何解决吗?代码更新如下。永远都在这里!

注意:ParksAnnotation,RestAnnotation,meetupAnn和StoreAnnotation都是MKPointAnnotation类。

更新(10/13/2020):我尝试在检索和地理编码“商店”坐标的代码块下记录“地标”。看起来好像好像在填充self.storeData一样,在创建的NSDictionary storeFields也是如此。就是说,当我登录“地标”时,即使已填充storeFields [@“ address”],它也不返回任何坐标。其他代码块似乎做得很好(例如,检索Meet Ups和检索Parks)。因此,Meet Ups,Restaurants和Parks批注看起来很好,但是并未填充所有Stores批注。当我启动应用程序时,至少会随机发生至少一种类型的注释(有时是缺少商店,有时是公园)。我一辈子都无法弄清为什么会这样。

请参见以下代码:

MapViewController.m

#import "StoreAnnotation.h"
#import "ParksAnnotation.h"
#import "RestAnnotation.h"
#import "meetupAnn.h"


@interface MapViewController ()

@end

@implementation MapViewController


      -(void)viewWillAppear:(BOOL)animated {
            

               NSMutableDictionary *viewParamsallUsers1 = [NSMutableDictionary new];
               [viewParamsallUsers1 setValue:@"hosted_meet_ups" forKey:@"view_name"];
               [DIOSView viewGet:viewParamsallUsers1 success:^(AFHTTPRequestOperation *operation,id responseObject) {
                   
                   self.meetUps = [responseObject mutableCopy];
                  
                   int index = 0;
                   
                   
                   for (NSMutableDictionary *allMeetups in self.meetUps) {
                       
                       NSString *location = allMeetups[@"where"];
                       NSString *userNames = allMeetups[@"node_title"];
                       NSString *userBio = allMeetups[@"body"];
                    
      
                       self.alertMessage = [allMeetups[@"node_title"] mutableCopy];
                       
                  
                       CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
                       [geocoderFriend geocodeAddressString:location
                                          completionHandler:^(NSArray* placemarks,NSError* error){
                                              if (placemarks && placemarks.count > 0) {
                                                  CLPlacemark *topResult = [placemarks objectAtIndex:0];
                                                  MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                                                  
                                                  MKCoordinateRegion region = self.mapView.region;
                                                  
                                                  region.span.longitudeDelta /= 150.0;
                                                  region.span.latitudeDelta /= 150.0;
                                                  
                                                  
                                                  meetupAnn *meet = [[meetupAnn alloc] init];
                                                  meet.coordinate = placemark.coordinate;
                                                  meet.title = userNames;
                                                  meet.subtitle = userBio;
                                                  meet.index = index;  // Store index here.
                                                  
                                                  [self.mapView addAnnotation:meet];
                                              }
                                          }
                        ];
                       
                       index = index + 1;
        
                   }
                
               
                   
               } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
                   NSLog(@"Failure: %@",[error localizedDescription]);
               }];
           
        
            
        
            
            /// GRAB ALL RESTAURANT LOCATIONS ///
            
            NSMutableDictionary *viewParams6 = [NSMutableDictionary new];
            [viewParams6 setValue:@"restaurants" forKey:@"view_name"];
            [DIOSView viewGet:viewParams6 success:^(AFHTTPRequestOperation *operation,id responseObject) {
                
                self.neighbourhoodData = [responseObject mutableCopy];
              
                int index = 0;
                
                
                for (NSMutableDictionary *multiplelocationsFriend in self.neighbourhoodData) {
                    
                
                    NSString *location = multiplelocationsFriend[@"address"];
                    NSString *userNames = multiplelocationsFriend[@"node_title"];
                     NSString *ampRemoved = [userNames stringByReplacingOccurrencesOfString:@"amp;" withString:@""];
                    
                    NSString *userBio = multiplelocationsFriend[@"body"];
                    self.x3 = multiplelocationsFriend[@"x3"];
             
                    
                    CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
                    [geocoderFriend geocodeAddressString:location
                                       completionHandler:^(NSArray* placemarks,NSError* error){
                                           if (placemarks && placemarks.count > 0) {
                                               CLPlacemark *topResult = [placemarks objectAtIndex:0];
                                               MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                                               
                                               MKCoordinateRegion region = self.mapView.region;
                                               
                                               region.span.longitudeDelta /= 150.0;
                                               region.span.latitudeDelta /= 150.0;
                                               
                                               
                                               RestAnnotation *point1 = [[RestAnnotation alloc] init];
                                               point1.coordinate = placemark.coordinate;
                                               point1.title = ampRemoved;
                                               point1.subtitle = userBio;
                                              
                                               point1.index = index;  // Store index here.
                                              
                                               
                                               [self.mapView addAnnotation:point1];
                                           }
                                       }
                     ];
                    
                    index = index + 1;
              
                }
         
                
            } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
                NSLog(@"Failure: %@",[error localizedDescription]);
            }];
            
            
        
            
            NSString *test = self.areaName;
            NSLog(@"SHOW TEST %@",test);
            
            
              /// GRAB ALL STORE LOCATIONS ///
               
               NSMutableDictionary *viewParams7 = [NSMutableDictionary new];
               [viewParams7 setValue:@"stores" forKey:@"view_name"];
               [DIOSView viewGet:viewParams7 success:^(AFHTTPRequestOperation *operation,id responseObject) {
                   
                   self.storesData = [responseObject mutableCopy];
                 
                   int index = 0;
                   
                   
                   for (NSMutableDictionary *storeFields in self.storesData) {
                       
                    
                       NSString *location = storeFields[@"address"];
                       NSString *userNames = storeFields[@"node_title"];
                        NSString *ampRemoved = [userNames stringByReplacingOccurrencesOfString:@"amp;" withString:@""];
                       
                       NSString *userBio = storeFields[@"body"];
                       self.x3 = storeFields[@"x3"];
                
                       
                       CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
                       [geocoderFriend geocodeAddressString:location
                                          completionHandler:^(NSArray* placemarks,NSError* error){
                                              if (placemarks && placemarks.count > 0) {
                                                  CLPlacemark *topResult = [placemarks objectAtIndex:0];
                                                  MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                                                  
                                                  MKCoordinateRegion region = self.mapView.region;
                                                  
                                                  region.span.longitudeDelta /= 150.0;
                                                  region.span.latitudeDelta /= 150.0;
                                                  
                                                  
                                                  StoreAnnotation *point2 = [[StoreAnnotation alloc] init];
                                                  point2.coordinate = placemark.coordinate;
                                                  point2.title = ampRemoved;
                                                  point2.subtitle = userBio;
                                                 
                                                  point2.index = index;  // Store index here.
                                                 
                                                  
                                                  [self.mapView addAnnotation:point2];
                                                  
                                               
                                                  
                                              }
                                          }
                        ];
                       
                       index = index + 1;
             
                   }
            
                   
               } failure:^(AFHTTPRequestOperation *operation,[error localizedDescription]);
               }];
               
               
            
                    /// GRAB ALL PARKS LOCATIONS ///
                         
                         NSMutableDictionary *viewParams8 = [NSMutableDictionary new];
                         [viewParams8 setValue:@"parks" forKey:@"view_name"];
                         [DIOSView viewGet:viewParams8 success:^(AFHTTPRequestOperation *operation,id responseObject) {
                             
                             self.parksData = [responseObject mutableCopy];
                          
                             
                             int index = 0;
                             
                             
                             for (NSMutableDictionary *parkFields in self.parksData) {
                                 
                                 //  NSLog(@"WHAT IS IN FRIENDS %@",self.friendData);
                                 
                                 NSString *location = parkFields[@"address"];
                                 NSString *userNames = parkFields[@"node_title"];
                                  NSString *ampRemoved = [userNames stringByReplacingOccurrencesOfString:@"amp;" withString:@""];
                                 
                                 NSString *userBio = parkFields[@"body"];
                                 self.x3 = parkFields[@"x3"];
                          
                                 
                                 CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
                                 [geocoderFriend geocodeAddressString:location
                                                    completionHandler:^(NSArray* placemarks,NSError* error){
                                                        if (placemarks && placemarks.count > 0) {
                                                            CLPlacemark *topResult = [placemarks objectAtIndex:0];
                                                            MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                                                            
                                                            MKCoordinateRegion region = self.mapView.region;
                                                            
                                                            region.span.longitudeDelta /= 150.0;
                                                            region.span.latitudeDelta /= 150.0;
                                                            
                                                            
                                                            ParksAnnotation *point3 = [[ParksAnnotation alloc] init];
                                                            point3.coordinate = placemark.coordinate;
                                                            point3.title = ampRemoved;
                                                            point3.subtitle = userBio;
                                                           
                                                            point3.index = index;  // Store index here.
                                                           
                                                            
                                                            [self.mapView addAnnotation:point3];
                                                            
                                                            
                                                         
                                                           
                                                        }
                                                    }
                                  ];
                                 
                                 index = index + 1;
                              
        
                             }
                       
                            
                         } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
                             NSLog(@"Failure: %@",[error localizedDescription]);
                         }];
                    
            }
        
          - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    
       static dispatch_once_t onceToken; dispatch_once(&onceToken,^{
        
       MKCoordinateRegion mapRegion;
        mapRegion.center = mapView.userLocation.coordinate;
        mapRegion.span.latitudeDelta = 0.5;
        mapRegion.span.longitudeDelta = 0.5;
        
        [mapView setRegion:mapRegion animated: YES];
        
        [self.locationManager stopUpdatingLocation];
        self.locationManager = nil;
    
            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,8000,8000);
           [mapView setRegion:[mapView regionThatFits:region] animated:YES];
            
           [mapView addAnnotations:[mapView annotations]];
    
            
           });
    }
    
        - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
           
         
            if([annotation isKindOfClass:[StoreAnnotation class]]) {
                static NSString *identifier = @"stores";
                MKAnnotationView *storesView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
                
                if(storesView == nil) {
                    
                   
                   storesView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
                    storesView.displayPriority = MKFeatureDisplayPriorityRequired;
                  
                   storesView.canShowCallout = YES;
                    storesView.image = [UIImage imageNamed:@"storeann4.png"];
                    
                  
                }
                
               else  {
                    
                   storesView.annotation = annotation;
                
          
            }
              
                return storesView;
               
            }
            
            if([annotation isKindOfClass:[meetupAnn class]]) {
                static NSString *identifier = @"meetUps";
                MKAnnotationView *pulsingView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
                
                if(pulsingView == nil) {
                    
                    pulsingView.displayPriority = MKFeatureDisplayPriorityRequired;
                    pulsingView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
                    pulsingView.image = [UIImage imageNamed:@"meetupbeacon.png"];
                    pulsingView.canShowCallout = YES;
               
               }
                
                else  {
                
                 pulsingView.annotation = annotation;
             
            }
              
                return pulsingView;
               
            }
                
                if([annotation isKindOfClass:[ParksAnnotation class]]) {
                    static NSString *identifier = @"parks";
                    MKAnnotationView *parksView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
                    
                    if(parksView == nil) {
                        
                      
                       parksView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
                        parksView.displayPriority = MKFeatureDisplayPriorityRequired;
                       parksView.image = [UIImage imageNamed:@"parksann.png"];
                        parksView.canShowCallout = YES;
                   
                    }
                    
                   else  {
                        
                        
                        parksView.annotation = annotation;
               
                }
                  
                    return parksView;
                   
                }
            
                if([annotation isKindOfClass:[RestAnnotation class]]) {
                    static NSString *identifier = @"rests";
                    MKAnnotationView *restView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
                    
                    if(restView == nil) {
            
                       restView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
                        restView.displayPriority = MKFeatureDisplayPriorityRequired;
                       restView.image = [UIImage imageNamed:@"restann.png"];
                       restView.canShowCallout = YES;
                   
                    }
                    
                else  {
                        
                        
                        restView.annotation = annotation;
                   
                }
                  
                    return restView;
                   
                }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)