显示自定义多个引脚显示位置错误的引脚

2023-12-13

这个问题已经困扰我好几个星期了!

我有一个标签栏应用程序。在一个选项卡上,我输入点,在另一个选项卡上,这些点显示在地图上。引脚应该根据点的类型而不同。

我面临的问题是,每次我从一个选项卡切换到另一个选项卡时,图钉图像都会从应有的图像更改为其他图像。例如,如果我在地图上有四个点,其中三个显示为圆形,一个显示为三角形,则三角形将从一个点移动到另一个点。图像的变化似乎相当随机。

所以,这是代码:

视图控制器.m

-(void) viewWillAppear:(BOOL)animated 
{
    // Select the type of map
    if (isMapSelected == NO) {
       self.mapView.mapType = MKMapTypeSatellite;
    }

    else {
       self.mapView.mapType = MKMapTypeStandard;
    }


    // Add region to the map (center and span)
    [self addRegion];

    // Removing old annotation
    [self.mapView removeAnnotations:mapLocations];

    // Initializing arrays for the annotations
    mapLocations = [[NSMutableArray alloc]init];

    [self addAnnotation];
}


-(void) addAnnotation 
{

   CLLocationCoordinate2D mapLocation;
   IGAMapAnnotation *mapAnnotation;

   // Calculate how many points are included
   NSInteger numberOfPoints = [coordinatesTempArray count];

   // Annotations will be added only of the flight plan includes at least one point
   if (numberOfPoints > 0) 
   {
     // Trying to add coordinates from the array of coordinates
     for (NSInteger i=0; i < ([coordinatesTempArray count]); i++) {

        mapAnnotation = [[IGAMapAnnotation alloc]init];

        // Taking a point in the array and getting its coordinates
        self.mapCoordinates = [coordinatesTempArray objectAtIndex:i];

        // Getting a point in the array and getting its lattitude and longitude
        self.mapLatitude = [[self.mapCoordinates objectAtIndex:0]doubleValue];
        self.mapLongitude = [[self.mapCoordinates objectAtIndex:1]doubleValue];

        // Assigning the point coordinates to the coordinates to be displayed on the map
        mapLocation.latitude = self.mapLatitude;
        mapLocation.longitude = self.mapLongitude;

        // Adding coordinates and title to the map annotation
        mapAnnotation.coordinate = mapLocation;
        mapAnnotation.title = [navaidNamesTempArray objectAtIndex:i];
        mapAnnotation.subtitle = nil;
        mapAnnotation.navaidType = [navaidTypesTempArray objectAtIndex:i];

        // Adding the annotation to the array that will be added to the map
        [mapLocations addObject:mapAnnotation];
    }

    // Adding annotations to the map
    [self.mapView addAnnotations:mapLocations];
    }
 }


-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{

   if([annotation isKindOfClass:[IGAMapAnnotation class]]) 
   {
     IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation;
     MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"IGAMapAnnotation"];

     if (annotationView == nil)
        annotationView = myLocation.annotationView;
     else
        annotationView.annotation = annotation;
     return annotationView;
   }
   else
      return nil;
}

IGAMapAnnotation.m

@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;
@synthesize type = _type;

// Tried to have this init method but was never able to make it work. Without it, the program crashes too!!!!

-(id)initWithTitle:(NSString *)newTitle Type:(NSString *)type Location:(CLLocationCoordinate2D) newCoordinate 
{
   self = [super init];

   if (self) {
       _title = newTitle;
       _coordinate = newCoordinate;
       _type = type;
   }

   return self;
}


-(MKAnnotationView *) annotationView {
   MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:@"IGAMapAnnotation"];
   annotationView.enabled = YES;
   annotationView.canShowCallout = YES;
   annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

   if ([self.type isEqual: @"A"] || [self.type isEqual: @"B"] || [self.type isEqual: @"C"]) 
   {
     annotationView.image = [UIImage imageNamed:@"circle.png"];
   }
   else if ([self.type isEqual: @"D"]) 
   {
     annotationView.image = [UIImage imageNamed:@"triangle.png"];
   }
   else if ([self.type isEqual: @"E"]) 
   {
     annotationView.image = [UIImage imageNamed:@"square.png"];
   }
   else 
   {
     annotationView.image = [UIImage imageNamed:@"oval.png"];
   }
   return annotationView;
}

@end

就是这个。到目前为止,这种行为对我来说毫无意义。 感谢您的帮助!


这听起来像是注释视图重用问题。

当重新显示注释时,它们会重新使用具有先前注释的图像的视图。这image视图中的属性没有被更新,因为当它被重新用于另一个注释时,它应该被更新。

In the viewForAnnotation委托方法,这段代码看起来错误:

MKAnnotationView *annotationView = [mapView dequeue...
if (annotationView == nil)
    annotationView = myLocation.annotationView;
else
    annotationView.annotation = annotation;

If the dequeue返回一个视图(即先前创建的视图,可能是为注释而创建的)不同的类型),其annotation属性已更新,但其image属性未更新。

现有代码仅设置image创建新注释视图时的属性(当dequeue回报nil).

现在,注释view创造和image-设置代码在注释中model class IGAMapAnnotation。最好创建一个自定义的MKAnnotationView自动更新的类image财产无论何时annotation属性已更新。

然而,另一种选择是将所有逻辑放在viewForAnnotation委托方法本身(并删除annotationView方法从IGAMapAnnotation class).

更新后的示例viewForAnnotation委托方法:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if (! [annotation isKindOfClass:[IGAMapAnnotation class]])
    {
        //return default view if annotation is NOT of type IGAMapAnnotation...
        return nil;
    }


    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"IGAMapAnnotation"];

    if (annotationView == nil)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"IGAMapAnnotation"];
        //these properties don't change per annotation 
        //so they can be set only when creating a new view...
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
    else
    {
        annotationView.annotation = annotation;
    }


    //whether we are using a completely new view or a re-used view,
    //set the view's image based on the current annotation...

    IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation;
    if ([myLocation.type isEqual: @"A"] || [myLocation.type isEqual: @"B"] || [myLocation.type isEqual: @"C"])
    {
        annotationView.image = [UIImage imageNamed:@"circle.png"];
    }
    else if ([myLocation.type isEqual: @"D"])
    {
        annotationView.image = [UIImage imageNamed:@"triangle.png"];
    }
    else if ([myLocation.type isEqual: @"E"])
    {
        annotationView.image = [UIImage imageNamed:@"square.png"];
    }
    else
    {
        annotationView.image = [UIImage imageNamed:@"oval.png"];
    }


    return annotationView;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

显示自定义多个引脚显示位置错误的引脚 的相关文章

  • 核心数据:为什么必须调用重新加载数据才能使我的应用程序运行?

    我花了整个晚上调试一个简单的应用程序 该应用程序从网络检索一张图像 是的 是的 旨在让我的生活更轻松 并将其显示在表格视图中 我这样做是为了练习学习核心数据 在我修复它之前 错误消息显示如下 2012 09 30 06 16 12 854
  • iOS 设置 MKMapView 中心,因此提供的位置位于底部中心

    我有一个 MKMapView 和一个永不改变的 CLLocationCooperative2D 我想做的是将地图居中 以便该坐标将放置在地图的底部中心 我可以用简单的方法将地图集中在这个坐标上 MKCoordinateRegion view
  • NSDateFormatter:根据 currentLocale 的日期,不包含年份

    这不会太难吧 我想显示不带年份的日期 例如 8 月 2 日 美国 或 02 08 德国 它也必须适用于许多其他语言环境 到目前为止 我唯一的想法是对年份进行正常格式 然后从生成的字符串中删除年份部分 我认为你需要看一下 NSString d
  • 将语音添加到自定义 UIMenuController

    我创建了一个自定义UIMenuController in a UIWebView但它似乎摆脱了 说出选择 选项UIMenuController在那之后 所有测试设备上的 偏好设置 中都打开了发言选择选项 并且它出现在其他应用程序中 包括非
  • 断点条件错误

    我已经根据条件设置了断点 event name isEqualToString Some Name 这很好用 但是 当我尝试添加另一个带有条件的断点时 part name isEqualToString Some Value With A
  • iOS 使用 NSColor 与 UIColor?

    有什么区别UIColor and NSColor 什么时候会使用每一种 我碰到NSColor在试图弄清楚的同时UIColor用于 iOS 中的属性字符串 我理解使用UIColor对于 UIKit 等 但我不认为NSColor对于这种事情确实
  • 使用 nib 作为带有 nib 类的表节标题

    我想创建一个加载 nib 文件并将其设置为标题 UIView 的节标题 这个 nib 文件还将有一个关联的类 其中插座和操作连接到 因此我想像平常一样使用 nib 加载该类 我在网上搜索并找到了几个类似的答案 但我找不到任何适合我的答案 经
  • 如何使用 SwiftUI 获取多个屏幕上的键盘高度并移动按钮

    以下代码获取键盘显示时的键盘高度 并将按钮移动键盘高度 在转换源 ContentView 和转换目标 SecibdContentView 处以相同的方式执行此移动 但按钮在转换目标处不移动 如何使按钮在多个屏幕上移动相同 import Sw
  • SceneKit unproject Z 文档解释?

    我正在经历一些 SceneKit 概念 而我试图在脑海中巩固的一个概念是 unprojectPoint 我知道该函数将获取 2D 中的一个点并返回 3D 中的一个点 因此具有正确的 Z 值 当我阅读文档时 我读到了以下内容 method u
  • 在 ios 版 Ionic 中接收 URL

    我正在使用离子框架 我正在尝试设置一种从另一个应用程序接收网址的方法 就像 您在浏览器中 单击共享 然后将链接发送到另一个应用程序 我的应用程序 我找到了这个cordova https stackoverflow com questions
  • iOS Swift 在后台下载大量小文件

    在我的应用程序中 我需要下载具有以下要求的文件 下载大量 例如 3000 个 小 PNG 文件 例如 5KB 逐个 如果应用程序在后台继续下载 如果图像下载失败 通常是因为互联网连接丢失 请等待 X 秒然后重试 如果失败Y次 则认为下载失败
  • 在 UITableViewController 中重新排序行后 UI 更新不正确

    因此 我对表中的行重新排序 用户界面最终结果不正确 场景如下 表内容原文 a b c d e 如果我移动第 0 行 当前a 到第 4 行 当前e 我看到的最终结果是 c d e a a 一些背景 该表正在读取 Realm 对象的列表 我确认
  • 如何将CIFilter应用到UIView上?

    根据Apple docs 过滤属性CALayer不支持iOS 当我使用正在申请的应用程序之一时CIFilter to UIView即 Splice Funimate 和 Artisto 的视频编辑器 Videoshow FX 这意味着我们可
  • 使用 BGTaskScheduler 进行后台获取与调试模拟完美配合,但在实践中却不起作用

    我在 appDelegate 的 didFinishLaunchingWithOptions 中注册后台获取任务 BGTaskScheduler shared register forTaskWithIdentifier Backgroun
  • Swift:协议、结构、类

    我开始学习 Swift 语言 但在理解协议 结构和类方面遇到了困难 我来自 Android 方面的编程 所以我相信 Swift 协议基本上是 Java 接口 其中每一个的正确用例是什么 这些类比并不 完全 正确 但这就是我所理解的要点 是的
  • 在 Swift 中以编程方式为 iOS 制作带有名字首字母的图像,例如 Gmail

    我需要在 UITableView 中显示与其姓名相对应的每个用户的个人资料图片 在下载图像之前 我需要显示一张带有他名字的第一个字母的图像 就像在 GMail 应用程序中一样 如何在 Swift for iOS 中以编程方式执行此操作 不需
  • 带有 allowedEditing 的 UIImagePickerController 不允许平移裁剪

    我在这里看到这个问题 UIImagePicker 允许编辑卡在中心 https stackoverflow com questions 12630155 uiimagepicker allowsediting stuck in center
  • removeItemAtPath 完成

    我正在以这种方式删除路径上的文件 UIPanGestureRecognizer gesture UIPanGestureRecognizer sender UIButton button UIButton gesture view UIPa
  • 子类 PFObject 上的 PFUser 属性

    我使用以下类 动态属性以及 m 文件中的 load 和 parseClassName 方法 对 PFObject 进行了子类化 interface DAOpponents PFObject
  • 无需越狱即可检测iOS9上哪个应用程序处于前台

    我正在尝试记录用户在 iOS9 上的个人应用程序使用情况 我宁愿它不会使用越狱有限的解决方案 不言自明 在越狱手机上执行此应用程序的变体应该不难 https www andyibanez com create mobilesubstrate

随机推荐

  • 与 PrimeFaces Converter 混淆(因为它适用于 selectOneMenu)

    AutoComplete demo 中 PlayersConverter 的实现实际上不仅充当转换器 还充当玩家列表的加载器 我对这个模型有点厌倦 因为加载已经在我的项目中实现了 我不明白为什么 Converter 接口没有作为模板实现 C
  • R 中的错​​误...缺少需要 TRUE/FALSE 的值[重复]

    这个问题在这里已经有答案了 以下是我的 R 脚本的一部分 for i in 1 N 1 if 50
  • Magento 管理网格将数据从 Action 发送到 Controller

    我正在尝试编写一个自定义操作来运行我构建的管理网格 是否可以通过 get 或 post 将网格中的列中的值发送到控制器 我尝试过谷歌搜索 但在任何地方都找不到正确的解释 如果可用的话 指向列设置 getter type 等 说明的链接也会很
  • 我应该何时为导出到 BigQuery 的 Firebase Analytics 数据运行每日 ETL 作业?

    我们使用 Firebase Analytics 从我们的应用收集事件 我们已启用将事件导出到 BigQuery 我们每天都会运行一些 ETL 作业 以便在 BigQuery 中创建更友好的分析表 例如会话 购买 问题是我们什么时候应该运行这
  • 通用 F# 函数:如何获取 F# 可辨别联合的类型?

    代码示例 http www tryfsharp org create dutts Generics fsx 我的 F 中有一些映射代码 它采用 C 对象并将其包装在可区分联合中 module MyModule type MappedThin
  • 在 PHP 中保护文件上传的好方法

    编写一个小应用程序 除其他事项外 让用户上传文件 例如图像 doc 或文本文件 作为他们发布 提交的一部分 我们当前的原型只是将文件转储到 app root 文件 但是当然 即使没有登录或使用该系统 任何人都可以访问该内容 目标是仅授予访问
  • 计算天、小时和分钟的时间差

    更新 我正在更新问题以反映完整的解决方案 使用下面提到的 time diff gem Brett 以下代码有效 code cur time Time now strftime Y m d H M Time diff Time parse 2
  • 如何精确匹配整个文档?

    精确匹配子文档很容易 但是有没有办法精确匹配集合中的整个文档 我有很多包含相似数据的文档 我只需要精确匹配 不需要额外的数据 使用负 exists 对我不起作用 因为我事先不知道所有可能的字段 我认为这不可能完全实现 但一个可能的解决方案是
  • 日志在生产中无法正常工作,作业延迟

    我遇到了一些奇怪的问题 我的delayed jobs 在生产中失败 最后我将范围缩小到记录器 如果我注释掉我的日志函数调用 一切都会正常 但是 如果我尝试记录 我会在delayed job处理程序中得到这个 ruby struct Dela
  • 通用静态字段初始化

    我只是对以下代码感到好奇 public static class Container
  • Swift 4 中的 UIButton 数组

    我用 UIButtons 在 UiKit 中制作了一系列复选框 IBOutlet weak var Box1 UIButton IBOutlet weak var Box2 UIButton IBOutlet weak var Box3 U
  • 如何摆脱 Chrome 控制台中的 [VM] 行?

    现在我可以在我的 chrome 开发者工具中看到有关 VM 的信息 如下所示 我找到了一些解决方案 例如将 暂停按钮 设为灰色 但是 它在我的开发工具中已经是灰色的 这对我来说也不起作用 如何消除控制台中的虚拟机消息 感谢您阅读我的问题 右
  • 当我尝试启动 jetty 时,为什么 lambda 表达式会破坏 guice 错误处理?

    我在尝试启动 jetty 时遇到以下问题 出现以下异常 Execution default test of goal org apache maven plugins maven surefire plugin 2 17 test fail
  • Xcode 中的 EXC_BAD_ACCESS 中断?

    我对 iPhone 开发和 Xcode 不太熟悉 不知道如何开始排除故障EXC BAD ACCESS信号 如何让 Xcode 在导致错误的确切行处中断 我似乎无法让 Xcode 在导致问题的线路上停止 但我确实在调试控制台中看到以下几行 1
  • Android 状态栏动画

    我是 Android 新手 我想做一个电池充电动画例如 在手机中 屏幕右上角的小图标在充电时会上下移动 并在当前电池百分比处停止 到目前为止 在我的代码中 我已经能够让它移动 但它永远不会停止 我想要的是动画在未充电时停止或以当前电池百分比
  • React refs 如何使用,何时使用?

    您好 感谢您阅读这个问题 我已经学习 React 几个星期了 我很难理解 refs 如何获取 React 的实例并将其放入 JS 变量中 例如 我们可以讨论文档的示例 class CustomTextInput extends React
  • 如何在Intellij Idea上导入滑动菜单?

    我正在使用intellij idea 如您所知 导入滑动菜单当你在 Eclipse 上运行时 将 lib 添加到你的全新项目中是很痛苦的 我做过一次 但我不再使用 intellij idea 我想知道是否有人知道如何在使用 Intellij
  • 在 R 中以全球视角绘制地理数据

    如何 是否可以在具有 d3 透视视图的地球上绘制地理数据 例如多边形层 类似于这个图形在维基百科上 我想要一个解决方案sf and ggplot大多数 但欢迎任何解决方案 我问这个主要是出于好奇 但由于我经常看到这样的图形 我想这个问题可能
  • Google API V3 多个信息窗口以及点击关闭

    我想出了如何使用多个带有信息窗口的标记 但当您单击另一个标记时它们不会关闭 我相信这是因为我正在为每个标记创建一个新的信息窗口 任何帮助将不胜感激
  • 显示自定义多个引脚显示位置错误的引脚

    这个问题已经困扰我好几个星期了 我有一个标签栏应用程序 在一个选项卡上 我输入点 在另一个选项卡上 这些点显示在地图上 引脚应该根据点的类型而不同 我面临的问题是 每次我从一个选项卡切换到另一个选项卡时 图钉图像都会从应有的图像更改为其他图