已经扫描所有类似的问题,并尝试所有可能的解决方案,我仍然无法找到我的情况的解决方案。我试图在我的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。
其实我已经实现了这个方法,它仍然不起作用。调试信息被添加到这个方法中,这就是为什么我说这个方法中的调试信息没有被打印在控制台中。 – Dreamer 2013-03-18 21:52:16
您发布的代码看起来大部分没问题。我对“TheActionMethod”有点怀疑,但是你说putPinsOnMap会被调用,所以你是从viewDidLoad的旧版本复制/粘贴的?你为什么不发布你的mapView:viewForAnnotation的实现。问题可能在于这种方法。 – 2013-03-18 21:56:45