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

时间:2014-08-26 19:49:15

标签: ios google-maps ios8 google-maps-sdk-ios

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

}

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:2)

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

添加代表:

GMSMapViewDelegate, CLLocationManagerDelegate

变量:

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

功能:

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
        }
    }

希望这会有所帮助。

答案 2 :(得分:0)

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

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