OGeek|极客世界-中国程序员成长平台

标题: ios - 更新 MKMapview 注释不起作用? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:09
标题: ios - 更新 MKMapview 注释不起作用?

我有一个带有 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 类别字段中的任何项目匹配。如果其匹配的 iam 在 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)numberOfComponentsInPickerViewUIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerViewUIPickerView *)pickerView numberOfRowsInComponentNSInteger)component{
    return [locations count];
}
-(NSString *)pickerViewUIPickerView *)pickerView titleForRowNSInteger)row forComponentNSInteger)component{
    return [locations objectAtIndex: row];
}
-(void)pickerViewUIPickerView *)pickerView didSelectRowNSInteger)row inComponentNSInteger)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 *)mapViewMKMapView *)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;
}



Best Answer-推荐答案


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

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

此调用完成后,只需将新注释添加到您的 map 。

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4