设置不同颜色的 MapKit 引脚

2024-04-12

我是 iOS 新手,我实现了一个 MapKit,其中包含来自不同数组类型的静态标记,并且它们工作正常,例如,我尝试将来自商店数组的引脚设置为蓝色,以及来自社区读取的引脚等。 .我不知道该怎么做 无论如何,它们在地图上都是红色的,我的目标是改变每个引脚阵列的颜色

这是我尝试过的:

import UIKit
import MapKit

class myMapViewController: UIViewController, MKMapViewDelegate {

    var shops = [Shops]()
    var communities = [Community]()
    var cyclists = [Cyclist]()
    var circuits = [Circuit]()
    
    @IBOutlet weak var myMap: MKMapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        shops.append(Shops(id: 0, title: "Shop1", latitude: 36.553015 , longitude: 10.592774))
        shops.append(Shops(id: 0, title: "Shop2", latitude: 35.499414 , longitude: 10.824846))
        communities.append(Community(id: 0, title: "community1", latitude: 37.276943 , longitude: 10.934709 ))
        communities.append(Community(id: 0, title: "community2", latitude: 35.427828 , longitude: 9.748186 ))
        circuits.append(Circuit(id: 0, title: "circuit1", latitude: 33.773035 , longitude: 10.857805 ))
        cyclists.append(Cyclist(id: 0, title: "cyclist1", latitude: 35.785118 , longitude: 10.000871 ))
        createShopsAnnotations(locations: shops)
        createCircuitsAnnotations(locations: circuits)
        createCommunityAnnotations(locations: communities)
        createCyclistsAnnotations(locations: cyclists)
    }
    
    func createShopsAnnotations(locations:[Shops]){
        for location in locations {
            let annotations = MKPointAnnotation()
            annotations.title = location.title as? String
            annotations.coordinate = CLLocationCoordinate2D(latitude: location.latitude as! CLLocationDegrees , longitude: location.longitude as! CLLocationDegrees)
            myMap.addAnnotation(annotations)
        }
    }
        
    func createCircuitsAnnotations(locations:[Circuit]){
        for location in locations {
            let annotations = MKPointAnnotation()
            annotations.title = location.title as? String
            annotations.coordinate = CLLocationCoordinate2D(latitude: location.latitude as! CLLocationDegrees , longitude: location.longitude as! CLLocationDegrees)
            myMap.addAnnotation(annotations)
        }
    }
    
    func createCommunityAnnotations(locations:[Community]){
        for location in locations {
            let annotations = MKPointAnnotation()
            annotations.title = location.title as? String
            annotations.coordinate = CLLocationCoordinate2D(latitude: location.latitude as! CLLocationDegrees , longitude: location.longitude as! CLLocationDegrees)
            myMap.addAnnotation(annotations)
        }
    }
    
    func createCyclistsAnnotations(locations:[Cyclist]){
        for location in locations {
            let annotations = MKPointAnnotation()
            annotations.title = location.title as? String
            annotations.coordinate = CLLocationCoordinate2D(latitude: location.latitude as! CLLocationDegrees , longitude: location.longitude as! CLLocationDegrees)
            
            myMap.addAnnotation(annotations)
        }
    }
}

我已经完成了一些教程,但无法将它们应用到我的示例中。


基本思想是创建一个注释视图类型来渲染正确的颜色。因此,假设您有四种注释类型(见下文),那么您可能有一个注释视图类型,如下所示:

class AnnotationView: MKMarkerAnnotationView {
    override var annotation: MKAnnotation? { didSet { update(for: annotation) } }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        update(for: annotation)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    func update(for annotation: MKAnnotation?) {
        switch annotation {
        case is Shop: markerTintColor = .blue
        case is Community: markerTintColor = .cyan
        case is Cyclist: markerTintColor = .green
        case is Circuit: markerTintColor = .red
        default: break
        }
    }
}

然后你可以让你的视图控制器为你的地图视图注册该注释视图:

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.register(AnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

    ...
}

请注意,当您注册这样的注释视图类型时,您会not想要实施任何mapView(_:viewFor:)方法。从 iOS 11 开始,不再需要/推荐该委托方法。

无论如何,注释视图类型假设您有四种类型的注释。我个人会做Shop, Community, Cyclist and Circuit只是注释类型,例如

class Shop: NSObject, MKAnnotation {
    let id: Int
    dynamic var title: String?
    dynamic var subtitle: String?
    dynamic var coordinate: CLLocationCoordinate2D

    init(id: Int, title: String, subtitle: String? = nil, coordinate: CLLocationCoordinate2D) {
        self.id = id
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate

        super.init()
    }
}

// repeat for the other three types

然后,当我想将它们添加到我的地图时:

shops.append(Shop(id: 0, title: "Shop1", coordinate: CLLocationCoordinate2D(latitude: 36.553015, longitude: 10.592774)))
shops.append(Shop(id: 0, title: "Shop2", coordinate: CLLocationCoordinate2D(latitude: 35.499414 , longitude: 10.824846)))
communities.append(Community(id: 0, title: "community1", coordinate: CLLocationCoordinate2D(latitude: 37.276943 , longitude: 10.934709)))
communities.append(Community(id: 0, title: "community2", coordinate: CLLocationCoordinate2D(latitude: 35.427828 , longitude: 9.748186)))
circuits.append(Circuit(id: 0, title: "circuit1", coordinate: CLLocationCoordinate2D(latitude: 33.773035 , longitude: 10.857805)))
cyclists.append(Cyclist(id: 0, title: "cyclist1", coordinate: CLLocationCoordinate2D(latitude: 35.785118 , longitude: 10.000871)))

mapView.addAnnotations(shops + communities + circuits + cyclists)

或者,您可能有一种注释类型:

enum PlaceType {
    case shop, community, cyclist, circuit
}

class Place: NSObject, MKAnnotation {
    let id: Int
    let type: PlaceType
    dynamic var title: String?
    dynamic var subtitle: String?
    dynamic var coordinate: CLLocationCoordinate2D

    init(id: Int, type: PlaceType, title: String, subtitle: String? = nil, coordinate: CLLocationCoordinate2D) {
        self.id = id
        self.type = type
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate

        super.init()
    }
}

然后您将实例化这些地方:

var places = [Place]()

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.register(AnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

    places = [
        Place(id: 0, type: .shop, title: "Shop1", coordinate: CLLocationCoordinate2D(latitude: 36.553015, longitude: 10.592774)),
        Place(id: 0, type: .shop, title: "Shop2", coordinate: CLLocationCoordinate2D(latitude: 35.499414 , longitude: 10.824846)),
        Place(id: 0, type: .community, title: "community1", coordinate: CLLocationCoordinate2D(latitude: 37.276943 , longitude: 10.934709)),
        Place(id: 0, type: .community, title: "community2", coordinate: CLLocationCoordinate2D(latitude: 35.427828 , longitude: 9.748186)),
        Place(id: 0, type: .circuit, title: "circuit1", coordinate: CLLocationCoordinate2D(latitude: 33.773035 , longitude: 10.857805)),
        Place(id: 0, type: .cyclist, title: "cyclist1", coordinate: CLLocationCoordinate2D(latitude: 35.785118 , longitude: 10.000871))
    ]
    mapView.addAnnotations(places)
}

然后您的注释视图类型将使用它type范围:

class AnnotationView: MKMarkerAnnotationView {
    override var annotation: MKAnnotation? { didSet { update(for: annotation) } }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        update(for: annotation)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    func update(for annotation: MKAnnotation?) {
        guard let annotation = annotation as? Place else { return }

        switch annotation.type {
        case .shop: markerTintColor = .blue
        case .community: markerTintColor = .cyan
        case .cyclist: markerTintColor = .green
        case .circuit: markerTintColor = .red
        }
    }
}

这导致:

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

设置不同颜色的 MapKit 引脚 的相关文章

  • iOS 13 检查 CLLocationManager 的临时授权状态

    根据 WWDC 视频 https developer apple com videos play wwdc2019 705 https developer apple com videos play wwdc2019 705 当你要求 Al
  • 如何在 Swift 中使用 CoreBluetooth 更新 BLE 设备的电池电量?

    func peripheral peripheral CBPeripheral didDiscoverCharacteristicsFor service CBService error Error for c in service cha
  • SceneKit unproject Z 文档解释?

    我正在经历一些 SceneKit 概念 而我试图在脑海中巩固的一个概念是 unprojectPoint 我知道该函数将获取 2D 中的一个点并返回 3D 中的一个点 因此具有正确的 Z 值 当我阅读文档时 我读到了以下内容 method u
  • 使用自动布局、IB 和字体大小时表头视图高度错误

    我正在尝试为我的 uiTableView 创建一个标题视图 不是节标题 我已经有了 我已经在界面生成器中设置了一个 XIB 所有的连接都已连接好并且运行良好 除了桌子没有给它足够的空间 我的问题是表格顶部与表格标题有一点重叠 我的 XIB
  • 为具有多个目标和不同平台的项目编写 Podfile

    我正在准备一个支持 OS X 和 iOS 的 Pod 我的 pod 有一些自己的依赖项 这些依赖项在 podspec 文件中定义 因此我使用 Podfile 来管理我用来开发 pod 和运行测试的项目的依赖项 我正在使用 CocoaPods
  • 与新 Apple Music 应用程序中相同的动态状态栏

    是否可以动态着色statusBar这是在新的苹果音乐应用程序 Edit iOS 8 4 中的新 Apple Music 应用程序具有此功能 打开应用程序 选择并播放歌曲 状态栏为白色 向下滑动播放器控制器以查看 我的音乐 控制器 它有黑色状
  • 在 ios 版 Ionic 中接收 URL

    我正在使用离子框架 我正在尝试设置一种从另一个应用程序接收网址的方法 就像 您在浏览器中 单击共享 然后将链接发送到另一个应用程序 我的应用程序 我找到了这个cordova https stackoverflow com questions
  • 在 UITableViewController 中重新排序行后 UI 更新不正确

    因此 我对表中的行重新排序 用户界面最终结果不正确 场景如下 表内容原文 a b c d e 如果我移动第 0 行 当前a 到第 4 行 当前e 我看到的最终结果是 c d e a a 一些背景 该表正在读取 Realm 对象的列表 我确认
  • 访问 google reader 的 Endpoints API 时出错

    我正在尝试在iPhone APP中实现google reader 到目前为止我已经成功收到了sid and auth 当我尝试使用以下命令调用 Endpoints API 时 问题就出现了GET 这是代码 ASIHTTPRequest re
  • 为arm64或arm7s编译OpenSSL FIPS功能库时出现未知的cpu类型

    我可以成功 至少没有警告并生成 a 文件 针对 arm7 x86 64 和 i386 进行编译 当我编译arm64时 我得到Unknown cpu type 100000c no adjustments made 当我编译arm7s时 我得
  • 循环多个 UIAlertController

    在某些情况下 我的应用程序需要显示多个警报消息 错误消息在启动时收集 并且需要一次向用户显示一条 当第一个被确认后 应该呈现下一个 问题在于 显然 它们都试图同时执行 有没有一种聪明的方法可以同步执行此操作 这是一些简单描述我想要做的事情的
  • 使用 ZBarSDK 时 iPhone 相机失去自动对焦功能

    我正在开发一个应用程序 用户可以选择是否要扫描条形码或拍摄某物的照片 为了拍照 我正在使用UIImagePickerController照常 为了扫描条形码 我使用 ZbarSDK 1 2ZBarReaderViewController 拍
  • UILabel 中的文本未垂直居中

    我使用以下代码创建了一个标签 func setupValueLabel valueLabel numberOfLines 1 valueLabel font UIFont name Avenir Black size 50 valueLab
  • iOS Swift 和 reloadRowsAtIndexPaths 编译错误

    我与 xCode Swift 陷入僵局并刷新 UITableView 的单行 这条线有效 self tableView reloadData 而这条线没有 self tableView reloadRowsAtIndexPaths curr
  • 如何在 iOS 上固定证书的公钥

    在提高我们正在开发的 iOS 应用程序的安全性时 我们发现需要对服务器的 SSL 证书 全部或部分 进行 PIN 操作以防止中间人攻击 尽管有多种方法可以做到这一点 但当您搜索此内容时 我只找到了固定整个证书的示例 这种做法会带来一个问题
  • cordova插件条码扫描仪打不开扫描

    我的条形码扫描仪插件有问题 我不是天才 我不太了解如何编写网络应用程序 我使用phonegap和cordova 并且尝试制作一个网络应用程序 在单击链接后扫描条形码 我之前已经使用此命令行安装了该插件 cordova plugin add
  • 当 ViewController 从 UIStoryboard 实例化时,isMemberOfClass 返回 no

    我有一个 OCUnit 测试类 PatientTestViewControllerTests 下面是界面 interface PatientTestViewControllerTests SenTestCase property nonat
  • 子类 PFObject 上的 PFUser 属性

    我使用以下类 动态属性以及 m 文件中的 load 和 parseClassName 方法 对 PFObject 进行了子类化 interface DAOpponents PFObject
  • 检查 Swift 中关联类型是否符合协议

    在类似情况下 如何检查对象是否符合 可表示 协议 protocol Representable associatedtype RepresentType var representType RepresentType get set cla
  • 如何观察UserDefaults的变化?

    我有一个 ObservedObject在我看来 struct HomeView View ObservedObject var station Station var body some View Text self station sta

随机推荐

  • Firestore没有执行操作的权限

    我正在尝试在 Firestore 中设置规则 如果每个人都经过身份验证进入应用程序 则每个人都可以读取彼此的内容 但只有文档所有者才能创建 写入 更新或删除它们 我在 Firestore 中设置了以下规则 rules version 2 s
  • Unity,如何将相机切换到第二个物体的位置?

    我在 Unity 3D 中遇到奇怪的问题 我的想法是找到数组中距离玩家最近和第二近的对象 然后我希望相机移动到最近的物体的位置并看着玩家 但如果玩家和最近的物体之间的距离太小 我希望相机移动到第二个物体的位置 我做了一些编码 但我不知道为什
  • 在 Silverlight 中使用 .NET RIA 服务有哪些陷阱?

    Silverlight可以使用WCF Web服务 基于REST的服务 NET RIA服务 但似乎Silverlight和 NET RIA服务是最受欢迎的 我想知道您在使用 NET RIA 服务实际实施 SL 时是否遇到过任何常见问题 如果继
  • 如何读取 RCFile

    我正在尝试将一个小的 RCFile 约 200 行数据 读入 HashMap 中以进行 Map Side 连接 但是在将文件中的数据变为可用状态时遇到了很多麻烦 这是我到目前为止所拥有的 其中大部分来自这个例子 http sumit1001
  • 如何从列表中选择每个第n个元素[重复]

    这个问题在这里已经有答案了 可能的重复 如何在 Haskell 中获取无限列表的每个第 N 个元素 https stackoverflow com questions 2026912 how to get every nth element
  • Java 流惰性 vs 融合 vs 短路

    我试图对 Java 流 API 中惰性求值的应用形成一个简洁而连贯的理解 目前我的理解是这样的 元素仅在需要时才被消耗 即流是惰性的 并且中间操作是惰性的 例如过滤器 仅在需要时进行过滤 中间操作可以融合在一起 如果它们是无状态的 短路操作
  • 具有多个字段的 Angular 模板驱动表单验证

    假设我有一个带有一些字段的简单表单 堆栈闪电战示例 https stackblitz com edit angular ktk7ez Component selector my app template h1 AppComponent h1
  • 使用基本 R 功能舍入 POSIX 日期 (POSIXct)

    我目前正在为我正在构建的包考虑很多日期和时间 绊倒这个post https stackoverflow com questions 8333838 how do you generate a sequence of the last day
  • 电子邮件解析云服务[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 似乎无法使用 Magit 推送到 GitHub

    我正在尝试在 Emacs 24 3 1 上使用 Magit 推送到 GitHub 存储库 请注意 我使用的是 Windows 盒子 我已经暂存并提交了对文件的更改 并且 M x magit status 仅反映驻留在我的存储库克隆中的未跟踪
  • xcodebuild 说不包含方案

    我有一个好奇心问题 我有一个项目 我一直在使用 XCode IDE 构建 并且运行良好 现在我正在设置 Bamboo 来构建项目 因此从命令行构建它 问题是 如果我从 GIT 中检查我的代码 然后使用 xcodebuild 来构建它 它会说
  • 如何对字符串中的子字符串重新排序?

    如何在 Perl 中的正则表达式中进行以下转换 British style US style 2009 27 02 gt 2009 02 27 我是 Perl 新手 对正则表达式了解不多 我能想到的就是提取 的不同部分 然后重新连接字符串
  • 使用 cassandra-cli 创建两个复合列

    我的列族需要两个复合列 关键数据类型是BytesType 以下是使用 CQL 的表定义 CREATE TABLE stats gid blob period int tid blob sum int uniques blob PRIMARY
  • 有没有办法用 EureakLog 进行正常日志记录?

    我正在使用 和旧版本 EurekaLog 它对于记录异常非常有用 但是有没有办法让它只正常记录事情呢 或者它根本不是为了这个目的 克里斯已经提到了我们的日志工具智能检测 http www gurock com smartinspect 谢谢
  • C++ 类设计、基类继承或外观设计模式

    我有一个愚蠢的 C 设计问题 有没有一种方法可以让一个类与多个类中的方法具有相同的方法名称 因此 具有相同的 API 我现在的情况是我有上课的情况 struct A void foo std cout lt lt A foo lt lt s
  • 如何继续 Subversion 中失败的加载

    我已经开始将颠覆转储加载到存储库中 在它完成之前 我耗尽了配额 命令停止了 我已要求更多配额 但现在我不知道如何继续进口 我应该简单地重做相同的命令吗 svnadmin load parent dir Software xxx module
  • Android UI - 动态添加按钮到 Gridview

    这就是我现在陷入困境的地方 我一直坚持向 gridview 动态添加按钮 我的 gridview 带有一个按钮 当用户单击该按钮时 会弹出上下文菜单 要求用户输入信息 完成后 使用该信息创建网格视图中的块 如图所示 我已经粘贴了代码 我不知
  • 如何更改 numpy 中的数组形状?

    如果我创建一个数组X np random rand D 1 它有形状 3 1 0 31215124 0 84270715 0 41846041 如果我创建自己的数组A np array 0 1 2 然后它就有了形状 1 3 看起来像 0 1
  • 如何将环境变量传递给 Jenkins 中的 sbt 测试构建步骤?

    在我的 scala 测试中 我通过以下方式读取了环境变量sys props getOrElse cassandra test host DEFAULT CASSANDRA TEST HOST 测试通过 Jenkins 运行 我添加了一个Bu
  • 设置不同颜色的 MapKit 引脚

    我是 iOS 新手 我实现了一个 MapKit 其中包含来自不同数组类型的静态标记 并且它们工作正常 例如 我尝试将来自商店数组的引脚设置为蓝色 以及来自社区读取的引脚等 我不知道该怎么做 无论如何 它们在地图上都是红色的 我的目标是改变每