【问题标题】:Trying to select an annotation which has not been added试图选择尚未添加的注释
【发布时间】: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


    【解决方案1】:

    在 MKMapView 上尝试 setCenterCoordinate 之前的 selectAnnotation

    例如:

    mapview.setCenterCoordinate(mapview.userLocation.coordinate, animated: false)
    mapview.selectAnnotation(mapview.userLocation,animated: true)
    

    更新:
    获取用户位置需要一些时间。
    MKMapViewDelegatedidUpdateUserLocation,它会在每次位置更新时更新。我们想在第一次触发时获取userLocation,然后选择它。

    ViewController 中创建一个bool var。这个var将用于第一次选择注解。我们想忽略didUpdateUserLocation 的所有其他回调。

    class MapViewController: UIViewController {    
        var didUpdateUserLocation = false
        ...
        ...
    }
    

    现在实现MKMapViewDelegatedidUpdateUserLocation函数。

    extension MapViewController : MKMapViewDelegate {
    
        func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
    
            if !didUpdateUserLocation {
                mapView.selectAnnotation(mapView.userLocation, animated: true)
                didUpdateUserLocation = true
            }
        }
    }
    

    【讨论】:

    • 感谢您提供这个不错的解决方法。但它不适用于 bool 变量。我改用 Int 。有关更多详细信息,请参阅下面的答案。无论如何,你让我走上了正轨:这是你的赏金 :)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      didupdatelocations 中设置布尔值不起作用,因为我必须等待viewforannotation 被调用。当主用户位置调用viewforannotation 时,我尝试输入布尔值,但它也不起作用(关于用户图像的奇怪错误)。我最终决定设置一个 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
                 }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-11
        • 2012-02-27
        • 1970-01-01
        • 2012-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多