2017-03-17 9 views
1

地図上に1カレンダーがあります。最初にView Controllerを開くと、現在の日付のすべての注釈が表示されます。これらの注釈をWebサービスから取得しています。ですから、カレンダーから日付を変更すると、Webサービスを呼び出す必要があり、選択した日付の新しい座標を取得します。私の問題は、現在の日付の注釈を追加することです。私のコードは、これは完全に追加され注釈on mapView

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; 

}

である私を助けてください。

答えて

1

はマップ

[_mapView clear] 

に新しいピンを追加する前にこの行を追加して、uが

+0

行わyou..Iがそれをチェックします感謝。 –