2013-02-17 55 views

回答

2

您需要在CLLocationManagerDelegate方法中调用webservice,请参阅低于

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
{ 
    CLLocationCoordinate2D coordinate = [newLocation coordinate]; 
    CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];  
    CLLocation *newlocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude]; 
    CLLocation *oldlocation = [[CLLocation alloc] initWithLatitude:oldCoordinate.latitude longitude:oldCoordinate.longitude]; 

    latitude =coordinate.latitude; 
    longitude =coordinate.longitude; 

    CLLocationDistance distDifference = [newlocation distanceFromLocation:oldlocation]; 
    int distance = distDifference; 
    if (distance>0.5) { 
    if (isInternetReachable==TRUE) { 
     [self callWebserviceSetlocationInBackground:newLocation]; 
     } 
    }  
    [newlocation release]; 
    [oldlocation release]; 
} 

    Also you need to set "Required background modes" Flag to "App registers for location updates" to use core location updated in background. 
    http://i.stack.imgur.com/WtcCm.png 
    This may help you. 

只需通过以下方法更改调用Web服务代码方法。我希望它能起作用: -

-(void) callWebserviceSetlocationInBackground:(CLLocation *)location 
    { 
     // REMEMBER. We are running in the background if this is being executed. 
     // We can't assume normal network access. 
     // bgTask is defined as an instance variable of type UIBackgroundTaskIdentifier 

     // Note that the expiration handler block simply ends the task. It is important that we always 
     // end tasks that we have started. 

    UIApplication *app = [UIApplication sharedApplication]; 

    UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask];}]; 

    NSLog(@"call web service in background lati = %f and long =%f",location.coordinate.latitude,location.coordinate.longitude); 


    [app endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
} 

谢谢。