【问题标题】:Swift - better way for Map AnnotationsSwift - 地图注释的更好方法
【发布时间】:2014-09-25 06:46:02
【问题描述】:

嘿,我目前正在开发一个小型应用程序,但我的内存使用率很高。 该应用程序有大约 200 个地图注释,这些被添加就像

    //Number: 1 Airport: Lukla  - Mount Everest
    let location1:CLLocationCoordinate2D = CLLocationCoordinate2DMake(AirportLat[1], AirportLong[1])
    let info1 = CustomPointAnnotation()
    info1.coordinate = location1
    info1.title = AirportTitle[1]
    info1.imageName = AirportimageName[1]
    Map.addAnnotation(info1)

    //Number: 2 Airport: Helgoland 
    let location2:CLLocationCoordinate2D = CLLocationCoordinate2DMake(AirportLat[2], AirportLong[2])
    let info2 = CustomPointAnnotation()
    info2.coordinate = location2
    info2.title = AirportTitle[2]
    info2.imageName = AirportimageName[2]
    Map.addAnnotation(info2)

    //Number: 3 Airport: Aspen 
    let location3:CLLocationCoordinate2D = CLLocationCoordinate2DMake(AirportLat[3], AirportLong[3])
    let info3 = CustomPointAnnotation()
    info3.coordinate = location3
    info3.title = AirportTitle[3]
    info3.imageName = AirportimageName[3]
    Map.addAnnotation(info3)

从我这里,所以我得到了一些数组,但它自己创建了每个注释(但所有“数组只是一个一个地计数”)我确信有更好的方法来做到这一点,谁能给我一些提示如何减少这个,也许为此使用 for/while? :( 我没有任何工作

【问题讨论】:

    标签: map swift annotations


    【解决方案1】:

    枚举其中一个数组,然后创建一个注释数组。然后,您可以一次将所有注释添加到地图视图中。

    var allAnnotations =  [CustomPointAnnotation]()
    for (index, value) in enumerate(AirportLat){
      let annotation = CustomPointAnnotation()
      annotation.coordinate = CLLocationCoordinate2DMake(AirportLat[index], AirportLong[index])
      annotation.title = AirportTitle[index]
      annotation.imageName = AirportimageName[index]
      allAnnotations += [annotation]
    }   
    
    Map.addAnnotations(allAnnotations)
    

    【讨论】:

    • 谢谢@insane-36 :) 该死的那么容易 :) 我是个白痴 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2016-02-16
    • 2017-06-11
    相关资源
    最近更新 更多