开发者

best way to get changed location iPhone sdk?

My ultimate goal is that i need to send lat and logitude to web server, every time when location changes.

I am using below code for sending lat and longitude of a device to a web server after every 2 minutes but it is not giving sometimes correct latitude and longitude as location changes.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
 *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    NSLog(@"Location Age%@",[NSString stringWithFormat:@"%d",locationAge]);
    if (locationAge > 5) return;

    // test that the horizontal accuracy does not indicate an invalid measurement    
    if (newLocation.horizontalAccuracy < 0) return;

    NSString *stringUrl;
   // BOOL check = FALSE;
    NSLog(@"Before condition condition");
    if(bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy){
        NSLog(@"condition");
         self.bestEffortAtLocation = newLocation;

        if (newLocation.horizontalAccuracy <= locmanager.desiredAccuracy) {
      开发者_StackOverflow中文版      // we have a measurement that meets our requirements, so we can stop updating the location
            // IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
            [locmanager stopUpdatingLocation];
            locmanager.delegate = nil;
        }
        //check = TRUE;
    }

    stringUrl = [NSString stringWithFormat:URLSAVELAT,stringUserId,[NSString stringWithFormat:@"%g",self.bestEffortAtLocation.coordinate.latitude],[NSString stringWithFormat:@"%g",self.bestEffortAtLocation.coordinate.longitude]];}

For location manager i am using below code

{
 locmanager = [[CLLocationManager alloc] init];
 [locmanager setDelegate:self];
 locmanager.distanceFilter = 10.0;
 //locmanager.distanceFilter = kCLDistanceFilterNone;
 [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
 [locmanager startUpdatingLocation];
}     

Any,even small help would be appreciated, Thank u in advance


You should use the location update call backs from the API and use updateLocation method:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
    [self.delegate locationUpdate:newLocation];
}
}

Then in the viewcontroller do this:

- (void)locationUpdate:(CLLocation *)location {

//DO WHATEVER YOU WANT HERE, INCLUDING SENDING TO SERVER

}

You also need to define two protocol methods, one of which is the locationUpdate:

 @protocol CoreLocationControllerDelegate
@required

- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;

@end

I would not recommend doing all that you do in the didUpdateLocation: method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜