CLLocation 管理器不工作 iOS 8
CLLocation manager not working iOS 8
我正在尝试学习这个 Google 地图教程:http://www.raywenderlich.com/81103/introduction-google-maps-ios-sdk-swift
像许多其他人一样,我遇到了一个障碍,其中 CLLocationManager
似乎没有触发 startUpdatingLocation()
。
我已根据 Location Services not working in iOS 8 将 pList
更新为 NSLocationAlwaysUsageDescription
和 NSLocationWhenInUseUsageDescription
,但仍然没有触发位置。
下面的代码 - 感谢您的帮助!
import UIKit
class MapViewController: UIViewController, TypesTableViewControllerDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: GMSMapView!
@IBOutlet weak var mapCenterPinImage: UIImageView!
@IBOutlet weak var pinImageVerticalConstraint: NSLayoutConstraint!
var searchedTypes = ["bakery", "bar", "cafe", "grocery_or_supermarket", "restaurant"]
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Types Segue" {
let navigationController = segue.destinationViewController as UINavigationController
let controller = segue.destinationViewController.topViewController as TypesTableViewController
controller.selectedTypes = searchedTypes
controller.delegate = self
}
}
// MARK: - Types Controller Delegate
func typesController(controller: TypesTableViewController, didSelectTypes types: [String]) {
searchedTypes = sorted(controller.selectedTypes)
dismissViewControllerAnimated(true, completion: nil)
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
println("success") // Works
locationManager.startUpdatingLocation() // Does not seem to fire
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("test") // Does not work
println(locations.count) // Does not work
if let location = locations.first as? CLLocation {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
难以置信。经过一天的测试,我找到了解决方案。代码很好。 模拟需要您更改位置
步骤:
- 在Xcode Simulator --> Debug --> Location --> Apple(或其他..)
- 重新运行模拟
Google地图现在将以您选择的位置为中心
在您的模拟设置中(Xcode --> 编辑方案 --> 允许
模拟 + 默认位置 = 纽约)
希望这对其他人有帮助!
代码似乎没问题,你也提到你已经更新了 info.plist。你能用 "requestAlwaysAuthorization" 而不是 "requestWhenInUseAuthorization" 检查吗?希望能解决你的问题。
你也可以参考这个link - http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
我正在尝试学习这个 Google 地图教程:http://www.raywenderlich.com/81103/introduction-google-maps-ios-sdk-swift
像许多其他人一样,我遇到了一个障碍,其中 CLLocationManager
似乎没有触发 startUpdatingLocation()
。
我已根据 Location Services not working in iOS 8 将 pList
更新为 NSLocationAlwaysUsageDescription
和 NSLocationWhenInUseUsageDescription
,但仍然没有触发位置。
下面的代码 - 感谢您的帮助!
import UIKit
class MapViewController: UIViewController, TypesTableViewControllerDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: GMSMapView!
@IBOutlet weak var mapCenterPinImage: UIImageView!
@IBOutlet weak var pinImageVerticalConstraint: NSLayoutConstraint!
var searchedTypes = ["bakery", "bar", "cafe", "grocery_or_supermarket", "restaurant"]
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Types Segue" {
let navigationController = segue.destinationViewController as UINavigationController
let controller = segue.destinationViewController.topViewController as TypesTableViewController
controller.selectedTypes = searchedTypes
controller.delegate = self
}
}
// MARK: - Types Controller Delegate
func typesController(controller: TypesTableViewController, didSelectTypes types: [String]) {
searchedTypes = sorted(controller.selectedTypes)
dismissViewControllerAnimated(true, completion: nil)
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
println("success") // Works
locationManager.startUpdatingLocation() // Does not seem to fire
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("test") // Does not work
println(locations.count) // Does not work
if let location = locations.first as? CLLocation {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
难以置信。经过一天的测试,我找到了解决方案。代码很好。 模拟需要您更改位置
步骤:
- 在Xcode Simulator --> Debug --> Location --> Apple(或其他..)
- 重新运行模拟
Google地图现在将以您选择的位置为中心 在您的模拟设置中(Xcode --> 编辑方案 --> 允许 模拟 + 默认位置 = 纽约)
希望这对其他人有帮助!
代码似乎没问题,你也提到你已经更新了 info.plist。你能用 "requestAlwaysAuthorization" 而不是 "requestWhenInUseAuthorization" 检查吗?希望能解决你的问题。
你也可以参考这个link - http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/