I am developing an app using swift and mapkit. The mapView has some annotations in addition to the mainUser annotation view. What I want is when the user selects a cell in a UITableViewCell from another ViewController, he should be brought to the mapView ViewController and a specific pin to be selected. I do this by passing an object called "fromSegue". All of this works great, except when the user wants to select itself: Selecting any kind of CustomPointAnnotationView is no problem, but when it comes to selecting the userLocation, the function ViewForAnnotation is not called yet.
Here is the function that sets the region of the map. This is where the annotation selection is supposed to occur.
func setFPS() {
print("[SFPS] - setFPS")
self.mainUser.setObject(mainUserLocation.latitude, forKey: "latitude")
self.mainUser.setObject(mainUserLocation.longitude, forKey: "longitude")
if self.fromSegue != nil {
print("[SFPS] - setModFPS")
if fromSegue.type == 11 {
print("[SFPS] - type11 - Showing a notif of type 11")
let latititi:Double = fromSegue.latitude as! Double
let longigigi:Double = fromSegue.longitude as! Double
self.mapScope = CLLocationCoordinate2DMake(latititi, longigigi)
var annotJar = pinJar.filter{$0.nickName == self.fromSegue.name}
print("[SFPS] - type11 - ALL RIGHT LETS DO IT")
let annot = annotJar[0]
Map.selectAnnotation(annot,animated: true)
fromSegue = nil
print("[SFPS] - type11 - Over")
} else if fromSegue.type == 21 {
print("[SFPS] - type21 - Showing a notif of type 21")
Map.selectAnnotation(self.Map.userLocation,animated: true)
self.mapScope = mainUserLocation
fromSegue = nil
} else {
print("[SFPS] - no type! problem here")
self.mapScope = CLLocationCoordinate2DMake(mainUserLocation.latitude, mainUserLocation.longitude)
}
} else {
self.mapScope = CLLocationCoordinate2DMake(mainUserLocation.latitude, mainUserLocation.longitude)
}
Map.rotateEnabled = true
let span = MKCoordinateSpanMake(0.001, 0.001)
print("[SFPS] - span made")
let region = MKCoordinateRegionMake(mapScope, span)
print("[SFPS] - region made")
Map.setRegion(region, animated: true)
print("[SFPS] - region is all set - Over")
}
When fromSegue is of type 11, it should select a customPointAnnotationview. And this is what happens in the console:
[SFPS] - setFPS
[SFPS] - setModFPS
[SFPS] - type11 - Showing a notif of type 11
[SFPS] - type11 - ALL RIGHT LETS DO IT
[VFA] - ViewForAnnotation
[VFA] - the annotation: <PING.CustomPointAnnotation: 0x1890d570>. from: Optional(Optional("newParse44"))
[VFA] - of kind random folk
[VFA] - of kind random folk - building...
[DSAV] - didSelectAnnotationView: Optional(Optional("newParse44"))
[DSAV] - guest
[DSAV] - Optional("newParse44") selected
[SFPS] - type11 - Over
[SFPS] - span made
[SFPS] - region made
[SFPS] - region is all set - Over
The function setFPS() is being executed, right when the annotation is about to be selected, it is created, then selected, then the rest of setFPS() is executed. Great! Everything works here.
Now when fromSegue is of type 21, the userLocation pin should be selected. And here is what happens in the console:
[SFPS] - setFPS
[SFPS] - setModFPS
[SFPS] - type21 - Showing a notif of type 21
2015-11-06 07:40:31.116 PING[1272:574013] ERROR: Trying to select an annotation which has not been added
[SFPS] - span made
[SFPS] - region made
[SFPS] - region is all set - Over
So in this case the userlocation annotation has not been added because viewForAnnotation has not been called (early enough?). My question is why? Could I call viewForAnnotation manually?
Thank you very much for your help