【发布时间】:2015-11-06 06:44:15
【问题描述】:
我正在使用 swift 和 mapkit 开发应用程序。除了 mainUser 注释视图之外,mapView 还有一些注释。我想要的是当用户从另一个 ViewController 中选择 UITableViewCell 中的一个单元格时,他应该被带到 mapView ViewController 并选择一个特定的引脚。我通过传递一个名为“fromSegue”的对象来做到这一点。所有这些都很好用,除非用户想选择自己:选择任何类型的 CustomPointAnnotationView 都没有问题,但是在选择 userLocation 时,还没有调用函数 ViewForAnnotation。
这里是设置地图区域的函数。这是注释选择应该发生的地方。
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")
}
当fromSegue为11类型时,应该选择customPointAnnotationview。这就是控制台中发生的事情:
[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
函数 setFPS() 正在执行,就在要选择注解时,创建它,然后选择它,然后执行 setFPS() 的其余部分。伟大的!一切都在这里工作。
现在当 fromSegue 的类型为 21 时,应该选择 userLocation 引脚。以下是控制台中发生的情况:
[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
所以在这种情况下,用户位置注释没有被添加,因为 viewForAnnotation 没有被调用(足够早?)。我的问题是为什么?我可以手动调用 viewForAnnotation 吗?
非常感谢您的帮助
【问题讨论】:
标签: ios swift mapkit mkannotation mkannotationview