Swift中的地图套件,第3部分-MKPointAnnotation,addAnnotations

Swift中的地图套件,第3部分-MKPointAnnotation,addAnnotations,swift,mapkit,ios8,mkannotation,mkpointannotation,Swift,Mapkit,Ios8,Mkannotation,Mkpointannotation,我正在尝试使用Swift中的地图工具包。我尝试在地图上显示该区域、一些接点(MKPointAnnotation)和当前位置。我决定扩展MKPointAnnotation类-添加一个类别 类MyAnnotation.swift: import UIKit import MapKit class MyAnnotation: MKPointAnnotation { var category = Int() } 我制作了一个元组数组来存储关于对象的信息。在循环中,我处理这个数组的所有元素,并从中生

我正在尝试使用Swift中的地图工具包。我尝试在地图上显示该区域、一些接点(MKPointAnnotation)和当前位置。我决定扩展MKPointAnnotation类-添加一个类别

类MyAnnotation.swift:

import UIKit
import MapKit

class MyAnnotation: MKPointAnnotation {
var category = Int() 
}
我制作了一个元组数组来存储关于对象的信息。在循环中,我处理这个数组的所有元素,并从中生成一个数组
MyAnnotation
。然后,我尝试使用
addAnnotations
显示整个数组MyAnnotation。但仅显示数组的最后一个元素

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mainMapView: MKMapView!

var locationManager = CLLocationManager()

var oneAnnotation = MyAnnotation()
var annotationArray = [MyAnnotation]()

var allObjectsTupleArray: [(objLat: CLLocationDegrees, objLong: CLLocationDegrees, objName: String, objDesc: String, objCat: Int)] = 
[(objLat: 53.204526, objLong: 50.111751, objName: "St. George's Church", objDesc: "Church of the Great Martyr St. George", objCat: 1), 
(objLat: 53.19364, objLong: 50.096599, objName: "Heart of Jesus Church", objDesc: "The Roman Catholic Church. Parish of the Sacred Heart of Jesus", objCat: 1), 
(objLat: 53.238906, objLong: 50.183754, objName: "Spiridonievsky Church", objDesc: "Parish of St. Spyridon the Wonderworker", objCat: 1), 
(objLat: 53.248509, objLong: 50.199976, objName: "St. John the Forerunner Church", objDesc: "Parish of of the Prophet and Forerunner St. John", objCat: 1), 
(objLat: 53.231875, objLong: 50.244071, objName: "The Resurrection Cathedral", objDesc: "The Resurrection Cathedral", objCat: 1)]

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    var currentLatitude = 53.2393
    var currentLongitude =  50.18145

    var latDelta = 0.05
    var longDelta = 0.05

    var currentLocationSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var currentLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(currentLatitude, currentLongitude)
    var currentRegion: MKCoordinateRegion = MKCoordinateRegionMake(currentLocation, currentLocationSpan)
    self.mainMapView.setRegion(currentRegion, animated: true)

    for oneObject in allObjectsTupleArray {

        var oneObjLoc: CLLocationCoordinate2D = CLLocationCoordinate2DMake(oneObject.objLat, oneObject.objLong)
        println("Latitude: \(oneObject.objLat)  Longitude: \(oneObject.objLong)")
        oneAnnotation.coordinate = oneObjLoc

        oneAnnotation.title = oneObject.objName
        println("ObjectName: \(oneObject.objName)")

        oneAnnotation.subtitle = oneObject.objDesc
        println("ObjectDescription: \(oneObject.objDesc)")

        oneAnnotation.category = oneObject.objCat
        println("Category: \(oneObject.objCat)")

        annotationArray.append(oneAnnotation)
    }

    self.mainMapView.addAnnotations(annotationArray)
}
}
我在
self.mainMapView.addAnnotations(annotationArray)
之前插入检查数组的内容
annotationArray

for testVar in annotationArray {
        println("Title of annotation: \(testVar.title)")
} 
此检查始终显示相同的标题-最后一个!为什么表达式
annotationArray.append(oneAnnotation)
填充最后一个值的数组?在Xcode 6中,对于阵列,Beta 5不推荐使用
+=


我做错了什么?

您只有MyAnnotation类的一个实例,因此总是将同一个对象附加到数组中

您必须在每个循环迭代中创建一个新的类实例

for oneObject in allObjectsTupleArray {
    let oneAnnotation = MyAnnotation()
    ...
}

该死!:-)在循环中移动了变量声明
var oneAnnotation:MyAnnotation=MyAnnotation()
-一切正常!谢谢,很抱歉问了这么愚蠢的问题!:-)我能再问一个问题吗?如何在地图上显示选定实例MyAnnotation的类别?我知道有必要使用函数
func-mapView(mapView:MKMapView!,annotationView:MKAnnotationView,calloutAccessoryControlTapped control:UIControl)
但是,如何引用类MyAnnotation的选定实例-我不知道。找到了解决方案!我将
func映射视图(映射视图:MKMapView!,注释视图:MKAnnotationView,calloutAccessoryControlTapped控件:UIControl)
此声明-
var selectedAnnotationView=annotationView.annotation as MyAnnotation
一切正常!