【发布时间】:2015-04-21 07:40:04
【问题描述】:
我已按照如何通过 Google Maps SDK 获取用户位置的教程中的步骤进行操作。我也使用了Ray Wenderlich 提供的谷歌教程,但没有用。 位置按钮甚至不显示。
任何帮助都会很棒,谢谢。 这是我的代码:
class NearestDealerViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mainMenuLabel: UILabel!
@IBOutlet weak var nearestDealer: UILabel!
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
let transitionDelegate = TransitionDelegate()
var delegate: NearestDealerViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
println(mapView.myLocationEnabled)
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
println(mapView.myLocationEnabled)
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
@IBAction func backTapped(sender: AnyObject) {
delegate?.dismissNearestDealerViewController()
}
}
【问题讨论】:
-
我认为您的地图在位置更新时未分配,因此在调用 didChangeAuthorizationStatus 时 nslog 地图视图
-
@chiragshah 我的 mapView 已连接到我的故事板。而且我还听取了您的建议,对我的 mapView 进行 nslog,它不是零。
-
你是在模拟器中运行这个项目然后你尝试改变位置。
-
在模拟器上你必须通过调试来改变你的位置 -> 位置它启用你的位置按钮
-
应该没问题,反正苹果模拟器在我的位置和它的按钮上存在已知问题。仍有疑问时,请进行测试。只需将其侧面加载到测试设备上并检查即可。
标签: ios google-maps swift cllocation