• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 更新 MKMapview 注释不起作用?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:09:55 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个带有 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap