2013-03-18 87 views
0

已经扫描所有类似的问题,并尝试所有可能的解决方案,我仍然无法找到我的情况的解决方案。我试图在我的MapView上放置多个针脚,但似乎无法正常添加我的针脚。我开始了一个NSTimer在我的viewDidLoad的方法,以便每隔5秒钟将一些更新的引脚放到Map上。我添加了调试信息,发现问题是viewForAnnotation方法没有被调用。 (我已经通过调用[_MAP setDelegate:自我]设置委托viewForAnnotation不被称为

我的代码如下:

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     _map = [[MKMapView alloc] initWithFrame:[[self view] frame]]; 
     [_map setDelegate:self]; 

     CLLocationCoordinate2D startLocation; 
     startLocation.latitude = [startLat floatValue]; 
     startLocation.longitude = [startLong floatValue]; 
     MKCoordinateSpan span = MKCoordinateSpanMake(0.002, 0.002); 
     MKCoordinateRegion region = MKCoordinateRegionMake(startLocation, span); 
     [_map setRegion:region]; 

     [[self view] addSubview:_map]; 

     [self getPlacesForLocation:[_map centerCoordinate]]; 

     NSTimer *currentTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(theActionMethod:) userInfo:nil repeats:YES]; 
     [currentTimer fire]; 
    } 

的getPlacesForLocationMethod:

-(void)getPlacesForLocation:(CLLocationCoordinate2D)location 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^
        { 

         /* get data from the website 
         ** get the geo information and put them in the MapPin struct 
         */ 

         [self putPinsOnMap]; 
        } 
} 

putPinsOnMap:

-(void)putPinsOnMap 
{ 

    for(Pinfo *iter in [_PinfoArray copy]) 
    { 
     MapPin *pin = [[MapPin alloc] init]; 
     [pin setTitle:[iter from_user_name]]; 
     [pin setSubtitle:[iter text]]; 
     [pin setCoordinate:[iter location]]; 

     //NSLog(@"The coordinate is %f %f", [pin coordinate].latitude, [pin coordinate].longitude); 

     [_map addAnnotation:pin]; 

     /****************************************/ 

     NSLog(@"Try to put the pin on Map"); 
     /****************************************/ 
    } 
} 

这里是我的MapPin.h内容:

@interface MapPin : NSObject<MKAnnotation> 

@property (nonatomic, copy) NSString *title, *subtitle; 
@property (nonatomic) CLLocationCoordinate2D coordinate; 

@end 

如果我留在原地,然后每次(无效)putPinsOnMap被调用,它打印“尝试把pin在地图上“viewForAnnotation方法没有被调用(我也在那里添加了调试信息,但没有打印)。只有几次,如果我在很大程度上缩小,有时会调用方法viewForAnnotation

回答

1

你说你添加了调试信息viewForAnnotation,但你没有subclassed MKMapView覆盖viewForAnnotation方法(或至少,你是分配一个MKMapView,而不是任何子类)。我认为你正在寻找的委托方法:

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

,如果你实现你的视图控制器方法(正在制作的的MKMapView的代表),你可能会得到有用的信息。

如果mapView:viewForAnnotation:除非缩放出来,否则您可能已经错误地放置了注释,而且它们只是在屏幕上的某个其他地方出现。

+0

其实我已经实现了这个方法,它仍然不起作用。调试信息被添加到这个方法中,这就是为什么我说这个方法中的调试信息没有被打印在控制台中。 – Dreamer 2013-03-18 21:52:16

+0

您发布的代码看起来大部分没问题。我对“TheActionMethod”有点怀疑,但是你说putPinsOnMap会被调用,所以你是从viewDidLoad的旧版本复制/粘贴的?你为什么不发布你的mapView:viewForAnnotation的实现。问题可能在于这种方法。 – 2013-03-18 21:56:45