MapKit in Swift, Part 3 - MKPointAnnotation, addAnnotations

亡梦爱人 提交于 2021-01-28 21:34:32

问题


I'm trying work with Map Kit in Swift. I try to display the area on the map, some pins (MKPointAnnotation) and the current position. I decided to extend the class MKPointAnnotation - add a category.

Class MyAnnotation.swift:

import UIKit
import MapKit

class MyAnnotation: MKPointAnnotation {
var category = Int() 
}

I made an array of tuples to store information about the objects. In the loop I process all the elements of this array and make from it an array MyAnnotation. Then, I try to display the entire array MyAnnotation using addAnnotations. But displays only the last element of the array....

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

I insert before self.mainMapView.addAnnotations(annotationArray) check the contents of the array annotationArray:

for testVar in annotationArray {
        println("Title of annotation: \(testVar.title)")
} 

This check is always displays the same title - the last one! Why expression annotationArray.append(oneAnnotation) fills the array of the last value? In Xcode 6 Beta 5 is deprecated += for arrays...

What am I doing wrong?


回答1:


You have only one instance of your MyAnnotation class so you are appending always the same object to your array.

You have to create a new instance of your class in each loop iteration.

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


来源:https://stackoverflow.com/questions/25237449/mapkit-in-swift-part-3-mkpointannotation-addannotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!