2014-12-03 133 views
1

我无法调用MKMapView委托方法didUpdateUserLocationSwift + didUpdateUserLocation没有被调用

我做了什么至今:

  1. 添加框架MapKit.framwork项目

  2. 进口框架中的视图控制器通过import MapKit线

  3. 添加键plist文件

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

  4. 012在视图控制器
  5. 代码

新增委托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键

enter image description here

也许可警报不会提示。

+0

关键'NSLocationAlwaysUsageDescription'的'value'必须是文本,即系统请求使用位置服务许可时提示用户的问题。 – zisoft 2014-12-03 10:31:19

回答

4
  1. 您必须保留对CLLocationManager的引用。
  2. 您必须等待locationManager(_:didChangeAuthorizationStatus:)代表方法调用,.startUpdatingLocation()showsUserLocation = true之前。
  3. 根据the documentNSLocationWhenInUseUsageDescription value type是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 
    } 

} 
+0

是的。我必须先调用'didChangeAuthorizationStatus' – Kampai 2014-12-03 10:45:27

1

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

NSLocationAlwaysUsageDescription(你已经有这个)

NSLocationWhenInUseUsageDescription

你也将需要结束检查应用程序是否在iOS8下运行,如果是这样添加下面两行。如果是iOS 7或以下,则需要跳过这两行。如果您忘记这么做,应用程序会在较低版本上崩溃。

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

包含两个密钥并不是必需的。 – zisoft 2014-12-03 10:29:18

0

的问题是,你是

locationManager.requestWhenInUseAuthorization() 

要求在使用时的授权,并在plist中要添加<key>NSLocationAlwaysUsageDescription</key>这是始终授权。您需要使用NSLocationWhenInUseUsageDescription重点

+0

尝试过仍然没有运气。 – Kampai 2014-12-03 10:30:56

+0

我不明白在这里downvotes的原因?这不是这个问题的一个可能的原因吗?并且应该注意在问题被更新之前给出了答案 – 2014-12-03 10:52:00

2

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

你把钥匙NSLocationAlwaysUsageDescriptioninfo.plist。该密钥的value必须是字符串包含问题的文本,当系统提示用户请求位置服务权限时。

根据关键,你必须通过调用

locationManager.requestWhenInUseAuthorization() 

你也许可以回答这样你的答案可能已经保存在用户偏好的问题请求的权限。最好的方法是从模拟器中彻底卸载应用程序并重新安装,以便再次提问。

+0

而且你是对的,我们必须为密钥'NSLocationAlwaysUsageDescription'传递字符串值。 – Kampai 2014-12-03 10:46:21

0

定了!

后iOS8上的MKMapView如果我们要使用的MKMapView装载更精确的位置, “-mapView:didUpdateUserLocation”:没有加载LocationCoordinate自动

前提: 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到窗口,因为在UtilSingleton代码,通常可以添加到您的控制器视图。

0

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