Swift 2 - 无法使用CLLocationManager获取用户位置

3

以下是我的VC代码:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    @IBOutlet weak var mapView: MKMapView!
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        mapView.showsUserLocation = true
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        mapView.showsUserLocation = (status == .AuthorizedAlways)
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }
}

我在plist文件中添加了NSLocationWhenInUseUsageDescription,同时也导入了CoreLocationMapKit框架,但是为什么这个功能不起作用呢?地图上没有显示用户位置,也没有输出用户坐标。在网上和Stack Overflow上都没有找到解决方法。


运行设置应用程序。滚动到应用程序列表的底部。选择您的应用程序设置。位置是否已禁用? - rmaddy
不,位置已启用。 - Ryan Westcott
1
刚刚注意到,你在 locationManager 上调用 startUpdatingLocation 方法的位置是哪里? - rmaddy
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) 中,检查状态是否为 AuthorizedWhenInUse,然后开始更新位置 :) - sloik
啊,那就是我的问题!我忘记调用 startUpdatingLocation 了,但地图上仍然没有显示用户位置。谢谢 @rmaddy - Ryan Westcott
3个回答

5

这对我有效

Swift 2 - (Xcode 7.2.1)

ViewController.swift


import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager: CLLocationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        self.locationManager.stopUpdatingLocation()

        let latestLocation = locations.last

        let latitude = String(format: "%.4f", latestLocation!.coordinate.latitude)
        let longitude = String(format: "%.4f", latestLocation!.coordinate.longitude)

        print("Latitude: \(latitude)")
        print("Longitude: \(longitude)")
    }
}

info.plist

添加一行新的代码

信息属性列表:NSLocationWhenInUseUsageDescription

类型:字符串

值:应用程序使用此信息来显示您的位置。


0

您需要在您的plist文件中使用以下位置访问权限:

  1. NSLocationAlwaysAndWhenInUseUsageDescription
  2. NSLocationWhenInUseUsageDescription

您没有使用始终和在使用中的权限。

Apple提示:

此应用程序尝试访问涉及隐私的数据,但未提供使用说明。该应用程序的Info.plist必须包含NSLocationAlwaysAndWhenInUseUsageDescription和NSLocationWhenInUseUsageDescription键,并提供字符串值,向用户解释应用程序如何使用这些数据。


0
尝试通过在方法locationManager(_:didUpdateLocations locations: [CLLocations])中获取数组locations的最后一个对象来获取位置。

就像这样:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue:CLLocationCoordinate2D = locations.last!
        print("locations = \(locValue.latitude) \(locValue.longitude)")
}  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接