iOS development-Baidu map background continuous positioning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.