Ios 正在尝试选择尚未添加的批注

Ios 正在尝试选择尚未添加的批注,ios,swift,mapkit,mkannotation,mkannotationview,Ios,Swift,Mapkit,Mkannotation,Mkannotationview,我正在使用swift和mapkit开发一个应用程序。除了mainUser注释视图外,mapView还有一些注释。我想要的是,当用户从另一个ViewController中选择UITableViewCell中的一个单元格时,他应该被带到mapView ViewController并选择一个特定的pin。我通过传递一个名为“fromSegue”的对象来实现这一点。所有这些都非常有效,除非用户想要选择自己:选择任何类型的CustomPointAnnotationView都没有问题,但是在选择userLo

我正在使用swift和mapkit开发一个应用程序。除了mainUser注释视图外,mapView还有一些注释。我想要的是,当用户从另一个ViewController中选择UITableViewCell中的一个单元格时,他应该被带到mapView ViewController并选择一个特定的pin。我通过传递一个名为“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
[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
因此,在这种情况下,没有添加userlocation注释,因为没有调用viewForAnnotation(是否足够早?)。我的问题是为什么?我可以手动打电话给viewForAnnotation吗


非常感谢您的帮助

在MKMapView上选择注释之前,请先尝试
设置中心坐标

例如:

mapview.setCenterCoordinate(mapview.userLocation.coordinate, animated: false)
mapview.selectAnnotation(mapview.userLocation,animated: true)
更新:
获取用户位置需要一些时间
MKMapViewDelegate
具有
didUpdateUserLocation
,它在每次位置更新时都会更新。我们希望在第一次触发时获取
userLocation
,然后选择它

ViewController
中创建
bool
var
。此
var
将用于首次选择注释。我们希望忽略
didUpdateUserLocation
的所有其他回调

class MapViewController: UIViewController {    
    var didUpdateUserLocation = false
    ...
    ...
}
现在实现
MKMapViewDelegate
didUpdateUserLocation
函数

extension MapViewController : MKMapViewDelegate {

    func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {

        if !didUpdateUserLocation {
            mapView.selectAnnotation(mapView.userLocation, animated: true)
            didUpdateUserLocation = true
        }
    }
}

如果要在选择自身时调用
ViewForAnnotation
,则必须在自身位置添加注释。单击系统注释时,它不会调用方法
ViewForAnnotation

多亏了@Warif提示,我想出了这个解决方案。虽然我知道这是一台可怕的戈德堡机器,但它完成了工作

didupdatelocations
中设置bool不起作用,因为我必须等待调用
viewforannotation
。当调用mainuserlocation的
viewforannotation
时,我试图设置bool,但它也不起作用(关于用户图像的奇怪bug)。我最终决定设置一个Int作为计数,以知道前面的所有步骤何时完成,并且我可以选择注释

class MapViewController: UIViewController {   
    var didUpdateUserLocation: Int = 0
    ...
    ...
    }

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if self.fromSegue != nil {
        if self.fromSegue.type == 21 {
            if self.didUpdateUserLocation == 0 {
                self.didUpdateUserLocation = 1
            }
        }
    }
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
       if self.fromSegue != nil {
           if self.fromSegue.type == 21 {
               if self.didUpdateUserLocation == 1 {
                   self.didUpdateUserLocation = 2
               }
           }
       }
    ...
    ...
    ...
    }


override func viewDidAppear(animated: Bool) {
    if self.fromSegue != nil {
       if self.fromSegue.type == 21 {
           if self.didUpdateUserLocation == 2 {
               Map.selectAnnotation(self.Map.userLocation,animated: true)
               self.fromSegue = nil
               self.didUpdateUserLocation = 3
           }
        }
    }
}

谢谢你这么好的解决方法。不过,它不适用于bool变量。我用了Int。有关更多详细信息,请参见下面的答案。无论如何,你让我走上了正确的道路:这是你的赏金:)