ios - 更新 MKMapkit View 注释不起作用?

标签 ios annotations nsarray mkmapview mapkit

我有一个带有 MKMapKit 和 PickerView.Like 的 UIViewcontroller,如下图所示。 UIViewcontroller 我有一个 plist,其中包含与 PickerView 中的值对应的所有坐标。因此,一旦我从 PickerView 中选择任何项目,我将刷新当前 map 并获取新项目的坐标(有时可能是 5 到 6 个坐标) 并在 map 中显示。

但对我来说,我可以看到“注释”即将进入 map View ,但是当我单击另一个项目时,之前的图钉仍然存在于 map 中。

我只想显示所选 PickerView 项目的注释图钉。请帮助我

用例: 这是我的 plist 文件 enter image description here

在我的 plist 中有一个名为“类别”的字段,Pickerview 中列出的项目是总类别列表。

因此,当我滚动 PickerView 时,我将检查所选项目是否与 Plist 类别字段中的任何项目匹配。如果其匹配,我会在 map View 中显示它们的坐标。

但是当我在 PickerView 中单击“restuarents”类别时,我得到了以下正确的 map 。

enter image description here

当我单击“cluburi”类别时,它应该在 map 中显示 2 个 Pin 图而不是 3 个。实际上它没有从 map 上清除当前的“restuarent”Pin。如下图 enter image description here

那么如何在显示新 Pin 图之前清除 map 中以前的 Pin 图?

这是我在 viewcontroller.m 文件中的代码

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [locations count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [locations objectAtIndex: row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

[_MAPVIEW setRegion:[_MAPVIEW region] animated:YES];

    for(int j=0;j<[[savedContentsarray valueForKey:@"category"] count];j++)
    {
        if([[[savedContentsarray valueForKey:@"category"] objectAtIndex:j] isEqualToString:[locations objectAtIndex: row]])
         {


              coordinate.latitude=[[latitude objectAtIndex:j] floatValue];
              coordinate.longitude=[[longitude objectAtIndex:j] floatValue];
              annotation = [[myAnnotation alloc] initWithCoordinate:coordinate title:[[savedContentsarray valueForKey:@"name"]objectAtIndex:j]];
              [_MAPVIEW addAnnotation:annotation];


        }

    }

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    //7
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //8
    static NSString *identifier = @"myAnnotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[_MAPVIEW dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView)
    {
        //9
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.pinColor = MKPinAnnotationColorPurple;
        annotationView.animatesDrop = YES;
        annotationView.canShowCallout = YES;
    }else {
        annotationView.annotation = annotation;
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}

最佳答案

所以在我的应用程序中,我创建了一个继承自 MKAnnotationView 的自定义类作为 btMapAnnotation。您必须在选择器控件中选择任何对象时进行此调用。

for (NSObject *a in [mapView annotations]) {
    if ([a isKindOfClass:[btMapAnnotation class]]) {
        btMapAnnotation *annotation = (btMapAnnotation *)a;
        [mapView removeAnnotation:annotation];
    }
}

此调用完成后,仅将新注释添加到 map 。

关于ios - 更新 MKMapkit View 注释不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20580718/

相关文章:

java - servlet 上的 Tomcat 类加载器

Java注解验证

objective-c - 我可以将同一个对象添加到两个不同的数组吗?

ios - iTunes Connect - 无效的 Swift 支持 - Watch OS 应用程序在两个位置都有 Swift 库

iPhone 标签栏应用程序不旋转

ios - 如何从 firebase 存储中删除文件夹?

Java 如何使用 JAXB 注释在 XML 中添加子元素

ios - 从空白行开始 UIPickerView

iphone - 字典值检索

ios - 如何制作具有多个列和标题的原生 iOS UIPicker?