我在地图上有一个日历。最初,当我打开视图控制器时,会显示当前日期的所有注释。我得到了这些注释'来自Web服务的坐标。因此,当我从日历更改日期时,我必须调用Web服务,并获得所选日期的新坐标。我的问题是,我正在添加当前日期的注释。我的代码是
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(addannotationwithtimerInitial:) userInfo:nil repeats:YES];
-(void) addannotationwithtimerInitial:(id)sender{
img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 60)];
if ([dateText stringForKey:@"dateTxt"] !=nil) {
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 60, 40)];
} else {
lbl = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 60, 40)];
}
if (annoCount< _lat.count-1) {
NSLog(@"anncount %d",annoCount);
double lat = [[_lat objectAtIndex:(annoCount)] doubleValue];
double longt = [[_longitude objectAtIndex:(annoCount)] doubleValue];
//CLLocationCoordinate2D loc;
loc.latitude = lat;
loc.longitude = longt;
NSLog(@"lac lat long %f %f",loc.latitude, loc.longitude);
Annotation.title = [_divDate objectAtIndex:annoCount];
NSLog(@" divv%@",[_divDate objectAtIndex:(annoCount)]);
Annotation = [[MKPointAnnotation alloc] init];
Annotation.coordinate = loc;
[_mapView addAnnotation:Annotation];
}
if (annoCount == 0) {
img.image = [UIImage imageNamed:@"ann1.png"];
float spanX = 0.5;
float spanY = 0.5;
MKCoordinateRegion region;
region.center.latitude = loc.latitude;
region.center.longitude = loc.longitude;
region.span.latitudeDelta = spanX;
region.span.longitudeDelta = spanY;
[self.mapView setRegion:region animated:YES];
}
else if (annoCount == _lat.count-1){
img.image = [UIImage imageNamed:@"ann1.png"];
[timer invalidate];
}
else{
img.image = [UIImage imageNamed:@"ann2.png"];
}
annoCount++;
}
这是完美的补充。但是当我更改日期时,我正在删除注释,然后我再次调用Web服务并显示注释。但是这次注释正在正确添加但是注释的值没有正确显示。它显示以前的值。我的代码是
[timer invalidate];
[_mapView removeAnnotation:Annotation];
for (id annotation in _mapView.annotations)
if (annotation != _mapView.userLocation)
[toRemove addObject:annotation];
[_mapView removeAnnotations:toRemove];
[self webservice];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(addannotationwithtimerInitial:) userInfo:nil repeats:YES];
并在地图方法上显示注释
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *reuseId = @"reuseid";
av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
//img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 60)];
[av addSubview:img ];
// UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 60, 40)];
// lbl1.text = [_divDate objectAtIndex:(annoCount)];
// NSLog(@"lbl txt %@",lbl1.text);
if ([dateText stringForKey:@"dateTxt"] !=nil) {
lbl1.backgroundColor = [UIColor clearColor];
lbl1.textColor = [UIColor whiteColor];
[lbl1 setFont: [lbl1.font fontWithSize: 12]];
[lbl1 setFont:[UIFont boldSystemFontOfSize:12]];
lbl1.text = [_divDate objectAtIndex:(annoCount)];
NSLog(@"lb tx %@",lbl1.text);
lbl1.alpha = 1;
[img addSubview:lbl1];
} else {
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
[lbl setFont: [lbl.font fontWithSize: 12]];
[lbl setFont:[UIFont boldSystemFontOfSize:12]];
lbl.text = [_divDate objectAtIndex:(annoCount)];
lbl.alpha = 1;
[img addSubview:lbl];
}
//Following lets the callout still work if you tap on the label...
av.canShowCallout = YES;
av.frame = lbl.frame;
}
else
{
av.annotation = annotation;
}
lbl = (UILabel *)[av viewWithTag:42];
//NSLog(@"ann %@",myAnnotation.title);
return av;
}
请帮帮我。
答案 0 :(得分:1)
在地图中添加新引脚之前添加此行
[_mapView clear]
你完成了