iOS开发 - 百度地图后台持续定位

  • 以前有作一个定位的项目,相似嘀嘀打车那样。 须要后台持续定位。html

    这里选择了百度地图,不过在后台持续定位方面, 以前只是简单的设置以下:java

    \

    不过经测试发现, 这样设置完,在后台运行大概30分钟,又会被crash掉。 从新打开应用则自动恢复定位。< 喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD48cD48YnIgLz48L3A+PHA+tbHIu6Os1eKyu8rHztLDx8/r0qq1xNCnufujrMv50tTV28zawcvPwqOsyrXP1sHLuvPMqLPW0Pi2qM67oaM8L3A+PHA+19y1xMC0y7WjrL7NysfA+9PDvfjI67rzzKi6887Sw8e/ybLZv9i1xDEwt9bW06OswLTN6rPJ0rvQqcrCx+mhozwvcD48cD7OqrTvtb2z1tD4tqjOu6Osw78xMLfW1tOho9fUtq/W2NDCv6rG9Laozruho9Xi0fm+zb3ivvbOysziwcuhozwvcD48cD48YnIgLz48L3A+PHA+vt/M5cjnz8I6PC9wPjxwPjxiciAvPjwvcD48cD5BcHBEZWxlZ2F0ZS5oPGJyIC8+PC9wPjxwPjxwcmUgY2xhc3M9"brush:java;">@property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier bgTask; 
    AppDelegate.m
    ios

    ?app

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    - ( void )backgroundHandler
    {
         NSLog(@ "### -->backgroundinghandler" );
         
         UIApplication* app = [UIApplication sharedApplication];
         
         bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
             
             [app endBackgroundTask:bgTask];
             bgTask = UIBackgroundTaskInvalid;
             
         }];
         
         // Start the long-running task
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
             
             // 您想作的事情,
             // 好比我这里是发送广播, 从新激活定位
             // 取得ios系统惟一的全局的广播站 通知中心
             NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
             //设置广播内容
             NSDictionary *dict = [[NSDictionary alloc]init];
             //将内容封装到广播中 给ios系统发送广播
             // LocationTheme频道
             [nc postNotificationName:@ "LocationTheme" object:self userInfo:dict];
     
         });
         
    }


    ?async

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    - ( void )applicationDidEnterBackground:(UIApplication *)application
    {
         // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
         
         BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout: 600 handler:^{ [self backgroundHandler]; }];
         
         if (backgroundAccepted)
             
         {
             NSLog(@ "backgrounding accepted" );
         }
     
         [self backgroundHandler];
    }




    而后在开启定位的位置,成为广播接收者,而且从新激活定位函数

    ?post

    1
    2
    3
    4
    5
    //初始化BMKLocationService
         myLocService = [[BMKLocationService alloc]init];
         myLocService.delegate = self;
         //启动LocationService
         [myLocService startUserLocationService];

    ?测试

    1
    2
    3
    4
    NSNotificationCenter *nc2 = [NSNotificationCenter defaultCenter];
         
         // 成为听众一旦有广播就来调用self recvBcast:函数
         [nc2 addObserver:self selector: @selector (activeLocation:) name:@ "LocationTheme" object:nil];


    ?ui

    1
    2
    3
    4
    5
    6
    7
    8
    9
    - ( void ) activeLocation:(NSNotification *)notify
    {
         [myLocService stopUserLocationService];
         //初始化BMKLocationService
         myLocService = [[BMKLocationService alloc]init];
         myLocService.delegate = self;
         //启动LocationService
         [myLocService startUserLocationService];
    }



    固然,上面的方式,可能方法比较渣,代码也写的比较乱。this

    只是提供一种解决办法而已。

相关文章
相关标签/搜索