适用于iOS的Google Maps API myLocationEnabled无效
我尝试使用Google Maps API,但无法获取用户的位置信息。观察值永远不会改变,因为observeValueForKeyPath永远不会被调用。
注意:我正在运行Xcode6-Beta 5和iOS8 beta(代码位于Swift中)
I am trying to use the Google Maps API but am having trouble getting the user's location. The observed value never seems to change since observeValueForKeyPath is never called.
Note: I am running Xcode6-Beta 5 and iOS8 beta (code is in 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.因此,要使用位置功能,您需要自行执行iOS 8位置授权。
As of right now, Google Maps iOS SDK hasn't been updated to support iOS 8. So in order to use the location features, you're going to need to do the iOS 8 location authorization yourself.
你可以通过实例化一个 CLLocationManager
对象并执行 -requestWhenInUseAuthorization
或 -requestAlwaysAuthorization
就可以了。您需要在将 myLocationEnabled
设置为 YES
之前执行此操作。
You can do this by instantiating a CLLocationManager
object and executing either -requestWhenInUseAuthorization
or -requestAlwaysAuthorization
on that. You'll need to do this before setting myLocationEnabled
to YES
.
一定要在 Info.plist
文件中包含必要的键。如果你想要始终使用授权(如在下面的代码示例),提供 NSLocationAlwaysUsageDescription
,或者如果要在使用时授权, NSLocationWhenInUseUsageDescription
。其中一个是必需的,如果你不提供它,位置功能将不起作用。
Be sure to also include the necessary keys in your Info.plist
file. If you are wanting to use always authorization (as in the code example below), provide NSLocationAlwaysUsageDescription
, or if you want when in use authorization, NSLocationWhenInUseUsageDescription
. One of them is required, and if you don't provide it, location features won't work.
这是Obj-C中的一个例子。对不起,我还没有和所有的Swift软件保持同步。 :)
Here's an example in Obj-C. Sorry, I'm not up to date with all the Swift stuff yet. :)
// 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;
}
}