【问题标题】:Swift + didUpdateUserLocation not getting calledSwift + didUpdateUserLocation 没有被调用
【发布时间】:2015-01-31 20:11:49
【问题描述】:

我无法调用MKMapView 委托方法didUpdateUserLocation

到目前为止我做了什么:

  1. 在项目中添加框架MapKit.framwork

  2. 通过import MapKit线在视图控制器中导入框架

  3. 在plist文件中添加key

    <key>NSLocationAlwaysUsageDescription</key> <true/>

  4. 视图控制器中的代码

添加了委托didUpdateUserLocation 方法来检查位置是否更新但从未调用。

// MARK: - MapView delegate methods

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
    // Calling...
}

func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
   // Not getting called
}

// MARK: - View lifeCycle methods   
override func viewDidLoad() {
    super.viewDidLoad()
    var locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()

    locationManager.startUpdatingLocation()

    self.mapView.delegate = self
    self.mapView.showsUserLocation = true
    self.mapView.pitchEnabled = true
    self.mapView.showsBuildings = true
}

我使用了模拟器并更改了模拟器自定义位置来检查它是否被调用?方法regionDidChangeAnimated被完美调用了!

同样的事情适用于 iOS7。 Swift 地图位置更新还有哪些额外的努力?

编辑: Plist 键

也未提示权限警报。

【问题讨论】:

  • 密钥NSLocationAlwaysUsageDescriptionvalue必须是文本,即当系统请求允许使用位置服务时,用户得到的提示问题。

标签: ios swift mapkit xcode6.0.1 mkuserlocation


【解决方案1】:

在尝试了以上所有答案后,如果“didUpdateUserLocation”委托没有被调用,那么选择模拟器,点击菜单栏中的调试,选择位置,然后选择苹果。我首先尝试了答案,但位置设置为自定义,所以我将其更改为 Apple 并调用了委托方法。之后我再次将其设置为自定义,现在它正在显示位置。

【讨论】:

    【解决方案2】:

    修好了!

    ios8后MKMapView没有自动加载LocationCoordinate。 如果我们想使用 MKMapView 加载更准确的位置,用“-mapView:didUpdateUserLocation”:

    前提: 1.设置mapView委托。 2.setShowsUserLocation:YES 3.mapView必须在一个容器中(addSubview:mapView)!!!!!

    例子:

    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    _mapView.delegate = self;
    [_mapView setShowsUserLocation:YES];
    [[UIApplication sharedApplication].keyWindow addSubview:_mapView];
    


    我将 MapView 添加到 Window 因为 UtilSingleton 中的代码,您通常可以添加到您的控制器视图中。

    【讨论】:

      【解决方案3】:

      问题是你要求什么时候使用授权

      locationManager.requestWhenInUseAuthorization()
      

      并在 pList 中添加 &lt;key&gt;NSLocationAlwaysUsageDescription&lt;/key&gt; 用于始终授权。你需要使用NSLocationWhenInUseUsageDescription

      【讨论】:

      • 尝试了同样的方法仍然没有成功。
      • 我不明白这里投反对票的原因?这不是问题的可能原因吗?并且应该注意,答案是在问题更新之前给出的
      【解决方案4】:

      对于在 iOS 8 上运行的应用程序,您需要在 plist 文件中添加两个键:

      NSLocationAlwaysUsageDescription(你已经有了这个)

      NSLocationWhenInUseUsageDescription

      此外,您还需要检查应用程序是否在 iOS8 下运行,如果是,请添加以下两行。如果是 iOS 7 或更低版本,则需要跳过这两行。如果您忘记执行此操作,应用程序将在较低版本上崩溃。

      // You have this check
      locationManager.requestWhenInUseAuthorization()
      // Add this one
      locationManager.requestAlwaysAuthorization()
      

      【讨论】:

      • 不需要同时包含两个键。
      【解决方案5】:
      1. 您必须保留对CLLocationManager 的引用。
      2. .startUpdatingLocation()showsUserLocation = true 之前,您必须等待locationManager(_:didChangeAuthorizationStatus:) 委托方法被调用。
      3. 根据the documentNSLocationWhenInUseUsageDescription值类型是String。

      试试:

      class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
      
          @IBOutlet weak var mapView: MKMapView!
      
          var locationManager = CLLocationManager()
      
          // MARK: - View lifeCycle methods
          override func viewDidLoad() {
              super.viewDidLoad()
              self.locationManager.delegate = self
              self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
              self.locationManager.requestWhenInUseAuthorization()
      
              self.mapView.delegate = self
              self.mapView.pitchEnabled = true
              self.mapView.showsBuildings = true
          }
      
          // MARK: - LocationManager delegate methods
      
          func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
              switch status {
              case .Authorized, .AuthorizedWhenInUse:
                  manager.startUpdatingLocation()
                  self.mapView.showsUserLocation = true
              default: break
              }
          }
      
          // MARK: - MapView delegate methods
      
      
          func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
              println(userLocation)
              // Not getting called
          }
      
      }
      

      【讨论】:

      • 是的。我得先打电话给didChangeAuthorizationStatus
      【解决方案6】:

      除了我想在这里总结的其他答案:

      您已将密钥NSLocationAlwaysUsageDescription 放入您的info.plist。当系统提示用户请求位置服务权限时,该键的value 必须是包含问题文本的字符串

      根据key,你必须通过调用来请求权限

      locationManager.requestWhenInUseAuthorization()
      

      您可能已经回答了该问题,因此您的答案可能已经保存在用户首选项中。最好的方法是从模拟器中完全卸载应用程序并重新安装,这样系统会再次询问您。

      【讨论】:

      • 你说得对,我们必须为键 NSLocationAlwaysUsageDescription 传递字符串值。
      猜你喜欢
      • 2015-03-30
      • 2015-03-07
      • 2015-05-17
      • 2015-10-21
      • 1970-01-01
      • 2014-12-27
      • 2017-02-11
      • 2014-09-22
      • 2021-11-12
      相关资源
      最近更新 更多