我已按照教程中的步骤操作,了解如何通过Google Maps SDK获取用户的位置。我也使用了Google提供的教程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()
}
}
答案 0 :(得分:1)
你应该放入viewDidLoad()下一行代码:
mapView.isMyLocationEnabled = true