适用于iOS的Google Maps API myLocationEnabled无法正常工作

Dan*_*sht 4 google-maps ios google-maps-sdk-ios ios8

我正在尝试使用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));
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 6

截至目前,Google Maps iOS SDK尚未更新以支持iOS 8.因此,为了使用位置功能,您需要自己进行iOS 8位置授权.

您可以通过实例化一个CLLocationManager对象并执行其中任何一个-requestWhenInUseAuthorization或此来执行-requestAlwaysAuthorization此操作.在设置之前,您需要执行此myLocationEnabled操作YES.

请务必在Info.plist文件中包含必要的密钥.如果您想要始终使用授权(如下面的代码示例所示),请提供NSLocationAlwaysUsageDescription,或者如果您想要在使用授权时,NSLocationWhenInUseUsageDescription.其中一个是必需的,如果您不提供,位置功能将不起作用.

这是Obj-C中的一个例子.对不起,我还没有及时了解所有Swift的东西.:)

// 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;
    }
}
Run Code Online (Sandbox Code Playgroud)