相关疑难解决方法(0)

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

这是我的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)")
    }
}
Run Code Online (Sandbox Code Playgroud)

我还在plist文件中添加了NSLocationWhenInUseUsageDescription.我有CoreLocation和MapKit框架,任何想法为什么这不起作用?它不会在地图上显示用户位置,也不会打印出用户的坐标.在堆栈溢出时没有在网上找到任何东西.

cllocationmanager ios swift xcode7 swift2

3
推荐指数
1
解决办法
4657
查看次数

标签 统计

cllocationmanager ×1

ios ×1

swift ×1

swift2 ×1

xcode7 ×1