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));
}
}
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.
You can do this by instantiating a
CLLocationManager
object and executing either-requestWhenInUseAuthorization
or-requestAlwaysAuthorization
on that. You'll need to do this before settingmyLocationEnabled
toYES
.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), provideNSLocationAlwaysUsageDescription
, 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.Here's an example in Obj-C. Sorry, I'm not up to date with all the Swift stuff yet. :)
Here is the code for others facing the same issue in iOS swift:
Add Delegates:
Variables:
Function:
Hope this will help.
Please don't forget to add this in Info.plist