首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >适用于iOS myLocationEnabled的谷歌地图应用程序接口不起作用

适用于iOS myLocationEnabled的谷歌地图应用程序接口不起作用
EN

Stack Overflow用户
提问于 2014-08-27 03:49:15
回答 3查看 6K关注 0票数 4

我正在尝试使用Google Maps API,但在获取用户的位置时遇到了问题。观察值似乎永远不会改变,因为observeValueForKeyPath从未被调用过。

注意:我运行的是Xcode6-Beta5和iOS8测试版(代码在Swift中)

代码语言:javascript
复制
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));
    }

}
EN

回答 3

Stack Overflow用户

发布于 2014-09-10 09:57:50

到目前为止,谷歌地图iOS软件开发工具包还没有更新到支持iOS 8。所以为了使用定位功能,你需要自己进行iOS 8的位置授权。

您可以通过实例化一个CLLocationManager对象并在该对象上执行-requestWhenInUseAuthorization-requestAlwaysAuthorization来完成此操作。在将myLocationEnabled设置为YES之前,您需要这样做。

请确保还在Info.plist文件中包含必要的密钥。如果您想要使用always authorization (如下面的代码示例所示),请提供NSLocationAlwaysUsageDescription,或者如果需要,在使用authorization时提供NSLocationWhenInUseUsageDescription。其中一个是必需的,如果你不提供它,定位功能将不起作用。

下面是一个使用Obj-C的示例。抱歉,我还不了解Swift的所有内容。:)

代码语言:javascript
复制
// 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;
    }
}
票数 6
EN

Stack Overflow用户

发布于 2014-10-25 19:08:03

以下是在iOS swift中遇到同样问题的其他人的代码:

添加委派:

代码语言:javascript
复制
GMSMapViewDelegate, CLLocationManagerDelegate

变量:

代码语言:javascript
复制
    @IBOutlet var gmaps: GMSMapView?
    var firstLocationUpdate: Bool?
    let locationManager = CLLocationManager()

功能:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-01-05 13:19:29

请不要忘记将其添加到Info.plist中

代码语言:javascript
复制
<key>NSLocationWhenInUseUsageDescription</key>
    <true/>
<key>NSLocationAlwaysUsageDescription</key>
    <true/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25514074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档