Google地图SDK for Xcode上的当前位置更新无效

我一直在试图在我的应用程序中实现谷歌地图。 我想要做的第一步是简单得到是:1.获取应用程序要求许可使用当前位置2.find用户当前的位置,并有一个蓝点标记来标记它。 3.更新并居中围绕用户当前位置的地图。

我已经尝试了各种方法来得到这个工作,但总是以一个错误或不工作。 最初,当我模拟下面的代码,它确实想出了请求当前位置权限和蓝点的对话框,但在下面的模拟中,这似乎已经消失了,当改变我在模拟器中的当前位置(通过编辑scheme和改变默认位置),新的当前位置没有更新。 我有一种感觉,代码不能超越viewDidLoad。 我哪里错了?

请给我一些关于如何纠正这些问题的build议,并获得以上几点工作(以及模拟器总是要求获得当前位置的许可)。 目前代码运行没有错误,但只显示了我的英国地图。 (我已经添加NSLocationWhenInUse和NSLocationAlwaysUsage ….到info.plist。我已经禁用了所有的断点,我也试过重置模拟器)

import UIKit import GoogleMaps class FirstViewController: UIViewController,CLLocationManagerDelegate { @IBOutlet weak var googlemaps: GMSMapView! //this is the connection from the storyboard to the view controller override func viewDidLoad() { super.viewDidLoad() let camera = GMSCameraPosition.camera(withLatitude: 51.508742, longitude: -0.087891, zoom: 8.0) let viewMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera) view = viewMap viewMap.isMyLocationEnabled = true let locationManager = CLLocationManager() locationManager.delegate = self locationManager.requestWhenInUseAuthorization() } override func loadView() { super.viewDidLoad() func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { // verification of granted permission while app in use let locationManager = CLLocationManager() let mapView = GMSMapView() if status == .authorizedWhenInUse { locationManager.startUpdatingLocation() // if permission granted- starting updating location mapView.isMyLocationEnabled = true mapView.settings.myLocationButton = true //this is suppose to be button that is added when tapped centers to users location } func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = locations.first { mapView.camera = GMSCameraPosition.camera(withTarget: location.coordinate, zoom: 20) self.view = mapView locationManager.stopUpdatingLocation() } } } } } 

提前致谢! 我花了几天试图解决这个没有成功:(

我能够通过更新Cocoapods并确保GoogleMaps在我的Podfile中来解决这个问题。 然后在我的viewController我有下面的代码完美执行。

  import UIKit import GoogleMaps class FirstViewController: UIViewController, CLLocationManagerDelegate { @IBOutlet weak var mapView: GMSMapView! var locationManager = CLLocationManager() var vwGMap = GMSMapView() override func viewDidLoad() { super.viewDidLoad() let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 22.300000, longitude: 70.783300, zoom: 10.0) vwGMap = GMSMapView.map(withFrame: self.view.frame, camera: camera) vwGMap.camera = camera locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyKilometer locationManager.distanceFilter = 500 locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() self.view = vwGMap } func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { if (status == CLAuthorizationStatus.authorizedWhenInUse) { vwGMap.isMyLocationEnabled = true } } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let newLocation = locations.last vwGMap.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 15.0) vwGMap.settings.myLocationButton = true self.view = self.vwGMap let marker = GMSMarker() marker.position = CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude) marker.map = self.vwGMap } }