适用于iOS的Google Maps API myLocationEnabled无效

我正尝试使用Google Maps API,但无法获取用户的位置信息。 观察值似乎永远不会改变,因为observeValueForKeyPath永远不会被调用。
注意:我正在运行Xcode6-Beta 5和iOS8 beta(代码是在Swift中)

override func viewDidLoad() { super.viewDidLoad() var camera:GMSCameraPosition? = nil mapView_ = GMSMapView.mapWithFrame(CGRectZero, camera:camera); mapView_!.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.fromRaw(0x01)!, context: nil); camera = GMSCameraPosition.cameraWithTarget(mapView_!.myLocation.coordinate, zoom: 6); mapView_!.camera = camera; self.view = mapView_; dispatch_async(dispatch_get_main_queue(), { mapView_!.myLocationEnabled = true; }); } override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafeMutablePointer<()>) { if (keyPath == "myLocation" && object.isKindOfClass(GMSMapView)) { mapView_!.animateToCameraPosition(GMSCameraPosition.cameraWithTarget(mapView_!.myLocation.coordinate, zoom: 6)); } } 

截至目前,Google Maps iOS SDK尚未更新为支持iOS 8.因此,要使用位置function,您需要自行执行iOS 8位置授权。

你可以通过实例化一个CLLocationManager对象,并对其执行-requestWhenInUseAuthorization-requestAlwaysAuthorization 。 在将myLocationEnabled设置为YES之前,您需要执行此操作。

一定要在Info.plist文件中包含必要的密钥。 如果您想要始终使用授权(如下面的代码示例中所示),请提供NSLocationAlwaysUsageDescription ,或者如果您希望在使用授权时使用NSLocationWhenInUseUsageDescription 。 其中一个是必需的,如果你不提供它,位置function将无法正常工作。

这是Obj-C中的一个例子。 对不起,我还没有与所有的Swift的东西date。 🙂

 // Rather than setting -myLocationEnabled to YES directly, // call this method: - (void)enableMyLocation { CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; if (status == kCLAuthorizationStatusNotDetermined) [self requestLocationAuthorization]; else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) return; // we weren't allowed to show the user's location so don't enable else [self.view setMyLocationEnabled:YES]; } // Ask the CLLocationManager for location authorization, // and be sure to retain the manager somewhere on the class - (void)requestLocationAuthorization { _locationAuthorizationManager = [[CLLocationManager alloc] init]; _locationAuthorizationManager.delegate = self; [_locationAuthorizationManager requestAlwaysAuthorization]; } // Handle the authorization callback. This is usually // called on a background thread so go back to main. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { if (status != kCLAuthorizationStatusNotDetermined) { [self performSelectorOnMainThread:@selector(enableMyLocation) withObject:nil waitUntilDone:[NSThread isMainThread]]; _locationAuthorizationManager.delegate = nil; _locationAuthorizationManager = nil; } } 

以下是iOS中swift中面临同样问题的其他代码:

添加代表:

 GMSMapViewDelegate, CLLocationManagerDelegate 

variables:

  @IBOutlet var gmaps: GMSMapView? var firstLocationUpdate: Bool? let locationManager = CLLocationManager() 

function:

 func startMaps() { locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.startUpdatingLocation() locationManager.requestWhenInUseAuthorization() let location: CLLocation = locationManager.location var target: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) var camera: GMSCameraPosition = GMSCameraPosition(target: target, zoom: 6, bearing: 0, viewingAngle: 0) if let map = gmaps? { map.myLocationEnabled = true map.camera = camera map.delegate = self } } 

希望这会有所帮助。

请不要忘记在Info.plist中添加这个

 <key>NSLocationWhenInUseUsageDescription</key> <true/> <key>NSLocationAlwaysUsageDescription</key> <true/>