Before there is a positioning project, similar to a beep taxi. Requires a background continuous positioning.
Baidu Map is selected here, but in the background of continuous positioning, before the simple setup is as follows:
However, after testing found that this setup, run in the background for about 30 minutes, and will be crash off. Re-opening the app restores the location automatically.
Of course, this is not the effect we want, so toss the next, to achieve the background of continuous positioning.
In general, we can do something by using the 10-minute control we have when we go backstage.
To achieve continuous positioning, every 10 minutes. Automatically re-open the location. This will solve the problem.
Specific as follows:
AppDelegate.h
@property (nonatomic, unsafe_unretained) Uibackgroundtaskidentifier bgtask;
Appdelegate.m
-(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), ^{ //What you want to do,// For example I'm here to send a broadcast, reactivate location //Get iOS system only Global Radio Notification Center nsnotificationcenter *NC = [ Nsnotificationcenter Defaultcenter]; Set broadcast content nsdictionary *dict = [[Nsdictionary alloc]init]; Encapsulate content into the broadcast Send broadcast //Locationtheme channel [NC postnotificationname:@ "Locationtheme" object:self UserInfo to iOS system: Dict];} ); }
-(void) Applicationdidenterbackground: (uiapplication *) application{ //Use the method to release shared resources, Save user data, invalidate timers, and store enough application state information to restore your application to its Curre NT 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];}
It then becomes the broadcast recipient at the location where it is opened, and activates the location
Initialize bmklocationservice mylocservice = [[Bmklocationservice alloc]init]; Mylocservice.delegate = self; Start Locationservice [Mylocservice Startuserlocationservice];
Nsnotificationcenter *NC2 = [Nsnotificationcenter defaultcenter]; To be a listener. Call self recvbcast: function [NC2 addobserver:self selector: @selector (activelocation:) name:@ " Locationtheme "Object:nil";
-(void) Activelocation: (nsnotification *) notify{ [Mylocservice stopuserlocationservice]; Initialize bmklocationservice mylocservice = [[Bmklocationservice alloc]init]; Mylocservice.delegate = self; Start Locationservice [Mylocservice startuserlocationservice];}
Of course, the above way, the method may be compared to the slag, the code is also written in a more chaotic.
It's just a way of providing a solution.
iOS development-Baidu map background continuous positioning