我成功地將自定義註釋添加到我的MKMapView併成功刪除它們。我正在使用子視圖控制器來過濾地圖視圖上的註釋。當我刪除所有註釋時,我在將新註釋添加到地圖視圖時遇到問題。正確的數據被添加到我的批註數組中,但是當我使用我的數組添加批註時,批註不會將其添加到地圖視圖中。任何幫助表示讚賞,謝謝。在iOS中,刪除所有自定義註釋並添加新的自定義註釋的正確方法是什麼?
這可能是值得一提的是,當我把自定義的註釋一個破發點if語句它被稱爲第一次我添加註釋到地圖視圖,而不是第二次。
這是我加上的註解:
for (placesChamber *placesObject in placesChamberArray) {
if ([placesObject.category isEqualToString:@"COC"]) {
tealAnnotation *annotationTeal = [[tealAnnotation alloc] init];
CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake([placesObject.latitude doubleValue],[placesObject.longitude doubleValue]);
annotationTeal.coordinate = coordinates;
annotationTeal.title = placesObject.name;
annotationTeal.tealAnnotationClass = placesObject;
[placesMapPointsArray addObject:annotationTeal];
}
}
[placesMapView addAnnotations:placesMapPointsArray];
NSLog(@"Number of Places found: %lu",(unsigned long)placesMapPointsArray.count);
NSLog(@"Number of Places on map: %lu",(unsigned long)placesMapView.annotations.count);
if (!placesMapView.annotations || !placesMapView.annotations.count) {
NSLog(@"There are 0 annotations.");
}//end if
這是使用我的自定義註釋:(MKAnnotationView *)的MapView:(的MKMapView *)theMapView viewForAnnotation:(ID)註釋
if ([annotation isKindOfClass:[tealAnnotation class]]) // for
{
// try to dequeue an existing pin view first
static NSString *TealAnnotationIdentifier = @"tealPin";
MKPinAnnotationView *tealPinView =
(MKPinAnnotationView *) [self.placesMapView dequeueReusableAnnotationViewWithIdentifier:TealAnnotationIdentifier];
if (tealPinView == nil)
{
// if an existing pin view was not available, create one
MKPinAnnotationView *tealAnnotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:TealAnnotationIdentifier];
tealAnnotationView.image = [UIImage imageNamed:@"teal_pin.png"];
tealAnnotationView.canShowCallout = YES;
tealAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
CGSize newSize;
newSize.width = 32;
newSize.height = 32;
UIGraphicsBeginImageContext(newSize);
[tealAnnotationView.image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
tealAnnotationView.image = newImage;
return tealAnnotationView;
}
else
{
tealPinView.annotation = annotation;
}
return tealPinView;
}
以下是我如何刪除註釋:
[placesMapView removeAnnotations:placesMapPointsArray];
[placesMapPointsArray removeObjectsInArray:placesMapPointsArray];
獲取信息後fr om註釋我已經注意到,我的mapView的viewDelegate已經變爲「無」。
嘗試更改'[placesMapView removeAnnotations:placesMapPointsArray];'''[placesMapView removeAnnotations:placesMap.annotations];' – JoeFryer
@JoeFryer沒有工作。仍然沒有向地圖視圖添加註釋。 – mcphersonjr
哪個if語句沒有被第二次觸發? –