【问题标题】:"StopUpdatingLocation" is called but GPS Arrow doesnt disappear调用“停止更新位置”但 GPS 箭头不消失
【发布时间】:2013-08-28 14:01:15
【问题描述】:

我不明白为什么在调用 stopUpdatingLocation 后灰色 gps 箭头不会消失。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.
    if (self.locationManager == nil)
    {
    self.locationManager = [[[CLLocationManager alloc] init]autorelease];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.delegate = self;
    }
    CLLocation *location = [self.locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    g_lat = coordinate.latitude;
    g_lng = coordinate.longitude;


    [self.locationManager startUpdatingLocation];
    }

这里是我的 didUpdateLocation:

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:  (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
    NSLog(@"Core location has a position.");

    CLLocationCoordinate2D coordinate = [newLocation coordinate];

    global_lat = coordinate.latitude;
    global_lng = coordinate.longitude;

    [self.locationManager stopUpdatingLocation];

    }

- (void) locationManager:(CLLocationManager *)manager
    didFailWithError:(NSError *)error
{

    NSLog(@"Core location can't get a fix.");
}

我还检查了是否有其他应用正在使用 GPS! 20分钟后,箭头仍然存在...... 感谢您的帮助!

编辑:

我想我错过了一些非常重要的东西,在应用程序启动后我的第一个视图中有一个谷歌地图!这是我的代码:

  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:g_lat
                                                            longitude:g_lng
                                                                 zoom:15];



    self.mapView = [GMSMapView mapWithFrame:CGRectMake(1.0f, 160.0f, 320.0f, 100)
                                     camera:camera];

    self.mapView.delegate = self;
    self.showsUserLocation = YES;
    self.mapView.trafficEnabled = YES;
    self.mapView.myLocationEnabled = YES;
    self.mapView.settings.myLocationButton = YES;
    self.mapView.settings.compassButton = YES;
    [self.mapView setUserInteractionEnabled:YES];
    [self.mapView setCamera:camera];

    [self.containerView addSubview:self.mapView];

谷歌地图有没有可能一直在更新?如果是,我该如何阻止?

【问题讨论】:

  • 我只是好奇,这是什么意思:NSLog(@"%@", @"Core location can't get a fix.");你可以直接写:NSLog(@"Core location can't get a fix."); 不需要争论。
  • 是的,你是对的,我改变了一些东西,忘了删除参数部分:)
  • 关于谷歌地图:当你推送新的视图控制器时,你是否关闭了myLocationEnabled字段?如果没有,那么这可以保持 GPS 运行。您可以通过不在下一个视图控制器上启动 GPS 来尝试。如果 GPS 保持打开状态,则说明地图正在保持它。
  • 这是问题所在,现在箭头消失了:)。我想接受这个作为正确答案,但似乎不可能,因为它是评论?
  • 我更新了我的答案,你可以接受它而不是评论。 :)

标签: iphone ios objective-c gps cllocationmanager


【解决方案1】:

关于谷歌地图:当你推送新的视图控制器时,你是否关闭了myLocationEnabled 字段?如果没有,那么这可以保持 GPS 运行。您可以通过不在下一个视图控制器上启动 GPS 来尝试。如果 GPS 保持开启状态,则说明地图正在保持它。

旁注:这可能是正常操作的一部分。如果您的应用程序停止接收位置更新,那么您做得很好。 iOS 做了很多优化,让 GPS 开启一段时间可能就是其中的一部分。在测试过程中,我看到即使应用程序被 XCode 杀死,箭头通常也会保持一段时间。

【讨论】:

  • 是的,它停止了更新。所以20分钟后箭头还在正常吗??
  • 哦,我错过了。 20分钟不容忽视。对我来说,不到一分钟,我想。至少我没有看到它在那里呆太久。
  • 尝试禁止所有其他应用程序使用定位服务:设置->隐私->定位服务并关闭所有应用程序。您甚至可以在此时重新启动设备以确保它是干净的。然后重新运行您的应用程序并包含一行以在您停止位置更新时释放位置管理器。如果您再次需要它,只需创建一个新的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多