-1

I have a map with annotations working fine. I want to have a segmentedcontrol to change the annotations.

this code

annotations = getMapAnnotations()
// Add mappoints to Map
mapInfView.addAnnotations(annotations )

works in the ViewDidLoad but I'm trying to implement it after changing the plist file to get the data from. I don't understand why I get Cannot assign value of type ['Stands'] to tupe MKAnnotation? when it's in the segmentedcontrol code and not in the viewdidload. Is there a better way of changing the annotations?

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

}

The stands Class

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

the IBAction code

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

}

cpmac
  • 69
  • 8
  • 1
    What _is_ `annotations`? You assign to it, but where / how is it declared? You must have a `var annotations` somewhere; show it. — Also, show your entire `viewDidLoad` where you claim this works. I bet it isn't the same. – matt Feb 15 '17 at 19:04

1 Answers1

0

matt was right. In ViewDid load I had

var annotations = getMapAnnotations()

in @IBAction func annotType()

I had

annotations = getMapAnnotations()

Adding var to declare it again solved the problem.

just after the class declaration, along with many other variable declarations I had

var annotations:MKAnnotations?

That seems to serve no purpose. I just commented it out and the app works fine.

cpmac
  • 69
  • 8