2014-09-25 74 views
0

嘿,我目前正在开发一个小应用程序,但是我的内存使用率却很高。 的应用得到了约200个地图标注,这些被添加像Swift - 映射注解的更好方法

//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) 

从我,所以我得到了一些阵列,但通过它创建每个注释自己的(但所有的“阵列算了算一个接一个”)我肯定有一个更好的方法来做到这一点,任何人都可以给我一些tipps如何减少这种情况,也许为此使用for/while? :(我没有得到阵列的任何工作

回答

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) 
+0

感谢@insane -36 :)该死的容易:) ima idiot :) – 2014-09-25 09:00:23