-1
我有一张带注释的贴图,效果很好。我想要有一个分段控件来更改注释。MKAnotation - 无法将typ x的值分配给MKAnnotation类型
这个代码
annotations = getMapAnnotations()
// Add mappoints to Map
mapInfView.addAnnotations(annotations)
作品在viewDidLoad中,但我想改变plist文件从获取数据后执行。 我不明白为什么我不能将类型['Stands']的值赋给tupe MKAnnotation?当它在分段控制代码中而不是在viewdidload中时。 有没有更好的方法来改变注释?
getannotations()
func getMapAnnotations() -> [Stands] {
var annotations:Array = [Stands]()
print("ANNOTATIONS")
//load plist file
var stands: NSArray?
print("(FILE \(iFile)")
if let path = Bundle.main.path(forResource: iFile, ofType: "plist") {
stands = NSArray(contentsOfFile: path)
print(path)
print(stands)
}
//iterate and create annotations
if let items = stands {
for item in items {
let lat = (item as AnyObject).value(forKey: "lat") as! Double
let long = (item as AnyObject).value(forKey: "long")as! Double
let annotation = Stands(latitude: lat, longitude: long)
let tit = (item as AnyObject).value(forKey: "title") as! String
let sub = (item as AnyObject).value(forKey: "subtitle") as! String
annotation.title = tit //"\(numb) \(tit)"
annotation.subtitle = sub
annotations.append(annotation)
}
}
return annotations
}
看台类
import Foundation
import MapKit
class Stands: NSObject, MKAnnotation {
var title: String?
var subtitle: String?
var no: Int?
var latitude: Double
var longitude:Double
var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
}
的IBAction为代码
@IBAction func annotType(_ sender: Any) {
let infoChoice = AnnotType(rawValue: infoSegmentController.selectedSegmentIndex)
switch (infoChoice!) {
case .WC:
iFile = "wc"
case .Restaurant:
iFile = "restaurant"
case .ATM:
iFile = "atm"
case .Museums:
print("Museums")
iFile = "museums"
}
print("END")
annotations = getMapAnnotations()
// Add mappoints to Map
mapInfView.addAnnotations(annotations)
}
什么_is_'annotations'?您分配给它,但它在哪里/如何声明?你必须在某处有一个'var annotations';展示下。 - 另外,显示你的整个'viewDidLoad'你声称这个作品。我敢打赌,这是不一样的。 – matt