簡體   English   中英

更改位置iPhone SDK的最佳方法?

[英]best way to get changed location iPhone sdk?

我的最終目標是每次位置更改時都需要將緯度和經度發送到Web服務器。

我使用下面的代碼每隔2分鍾將設備的經度和緯度發送到Web服務器,但是隨着位置的變化,有時它不能給出正確的經度和緯度。

- (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) {
            // 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]];}

對於位置管理器,我正在使用以下代碼

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

任何,甚至很小的幫助,將不勝感激,在此先感謝您

您應該使用API​​中的位置更新回調,並使用updateLocation方法:

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

然后在視圖控制器中執行以下操作:

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

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

}

您還需要定義兩種協議方法,其中一種是locationUpdate:

 @protocol CoreLocationControllerDelegate
@required

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

@end

我不建議您在didUpdateLocation:方法中進行所有操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM