2010-06-22 4 views
4

사용자가 이동 한 거리를 계산하는 응용 프로그램을 개발 중입니다. 이렇게하려면 CLLocationManager 클래스를 사용하고 있지만 처음에는 캐시 된 데이터를 가져오고 거리 변수도 갑자기 증가합니다. 제발 도와주세요 ... 다음 코드를 사용했습니다 ....Corelocation의 잘못된 거리

참고 : 거리는 정적 var입니다. 여기

- (void)viewDidLoad { 
    [super viewDidLoad]; 
//bestEffortAtLocation = nil; 
oldLocat = [[CLLocation alloc]init]; 
newLocat = [[CLLocation alloc]init]; 
locationManager =[[CLLocationManager alloc]init]; 
locationManager.delegate = self; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[locationManager startUpdatingLocation]; 

    } 

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation{ 




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


NSLog(@"accuracy %d",newLocation.horizontalAccuracy); 

// test the age of the location measurement to determine if the measurement is cached 
// in most cases you will not want to rely on cached measurements 
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 
//NSLog(@"time %d",locationAge); 

if (locationAge > 5.0) return; 



self.oldLocat = oldLocation; 
self.newLocat = newLocation; 


double latDegrees = newLocation.coordinate.latitude; 
NSString *lat = [NSString stringWithFormat:@"%1.5f°",latDegrees]; 
latLabel.text = lat; 
double longDegrees = newLocation.coordinate.longitude; 
NSString *longt = [NSString stringWithFormat:@"%1.5f°",longDegrees]; 
longLabel.text = longt; 
[self computeDistanceFrom:oldLocat tO:newLocat]; 

    } 

    -(void)computeDistanceFrom:(CLLocation *)oldL tO:(CLLocation *)newL 
{ 
NSLog(@"oldd %@",oldL); 
NSLog(@"new %@",newL); 


distance = distance + [oldL getDistanceFrom:newL]; 
NSLog(@"distance %f",distance); 

} 
콘솔은 다음 데이터를 표시

.......

정확도 oldd 0 (NULL) 28.62114850 < 새로운 77.37001021가> +/- 80.00m (속도 MPS -1.00/-1.00 코스) 거리 2010-06-22 0,530 19시 21분 59초 @ 0.000000

정확도 oldd < 28.62114850 0 77.37001021> +/- 80.00m (-1.00 속도 MPS/코스 -1.00) @ 2010-06-22 19:21:59 +0530 신규,245,080,543,210 28.61670485, 77.37068155> +/- 80.00m (MPS 속도 -1.00/-1.00 코스) 거리 498.211345

정확도 0 oldd < 28.61670485 19시 22분 0초 2010-06-22 0,530 @ +77.37068155> +/- 80.00m (속도 -1.00 mps/코스 -1.00) @ 2010-06-22 19:22:00 +0530 신규 < +28.62112748, +77.36998540> +/- 80.00m (속도 -1.00 mps/course -1.00) @ 2010-06-22 19:23:02 +0530 distance 994.432508

답변

7

처음에는 이전에 캐시 된 위치를 얻는 것이 정상입니다. CLLocationtimestamp을 보면 오래된 캐시 된 데이터를 무시할 수 있습니다.

정확도가 정확하지 않고 % f가 아닌 % d를 사용하고 있습니다. 유형이 int가 아닌 double입니다.

셀 삼각 측량의 정확도가 낮기 때문에 GPS가 처음 시작될 때 위치가 빠르게 변경 될 수 있습니다. 그러면 GPS 획득을 통해 더 높은 정확도의 위치를 ​​얻을 수 있습니다. 그것들은 멀리 떨어져 있어도 (1000m), 당신이 몇 초 안에 멀리 움직 였지만 정확도 만 바뀌었다.

이동 거리 계산의 정확도가 다른 두 위치를 사용하지 마십시오.

EDIT 이전 위치 데이터를 무시하는 코드 샘플이 추가되었습니다. 무시할 나이를 결정하면 여기에 60 초를 사용합니다.

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation { 

    NSTimeInterval ageInSeconds = -[newLocation.timestamp timeIntervalSinceNow]; 
    if (ageInSeconds > 60.0) return; // data is too long ago, don't use it 

    // process newLocation 
    ... 
} 
+0

감사합니다 progrmr하지만 정확히 타임 스탬프를 사용하여 캐시 된 데이터를 제거하는 방법을 말해 줄 수 있습니다 ........ 그리고 거리를 계산하는 위의 접근 방식이 잘못된 경우 어떻게 진행해야합니까 ...... 거기에 있습니다. 거리를 계산하는 다른 방법 ..... coz 나는 거리, 속도 등등을 계산하는 것과 같은 일을하는 앱 스토어에서 많은 앱을 보았습니다. 당신의 소중한 제안을주세요. – Siddharth

+1

오래된 캐시 데이터를 제거하는 방법을 보여주기 위해 코드 샘플을 추가했지만 코드에서 이미 수행하고 있습니다. – progrmr

2

이미 위치 업데이트가 5 초 미만인 것으로 확인되었습니다.

progrmr에서 언급 한 바와 같이
if (locationAge > 5.0) return; 

, 키 문제는 여기에 거의 확실히 당신이 낮은 정확도를하고 그래서 위치가 표시되는 위치의 초기 추정에 의존하고 있다는 점이다 : 그것은 코드 줄이하는 일입니다 귀하의 위치에 대한 더 나은 수정 사항을 얻으면서 빠르게 이동하십시오. 먼저 정확한 위치 픽스가있을 때만 oldLocation이 설정되도록하고 그런 다음 newLocations가 허용되는 해상도 인 경우 해당 oldLocation과 비교합니다.당신이 방법 computeDistanceFrom을 필요로하지 않습니다 위의 코드

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation{ 

    // Ignore location updates that are less than 10m accuracy, or where the horizontalAccuracy < 0 
    // which indicates an invalid measurement. 
    NSLog(@"New location accuracy %.0fm", newLocation.horizontalAccuracy); 
    if ((newLocation.horizontalAccuracy < 0) || (newLocation.horizontalAccuracy > 10)) return; 

    // Ignore location updates that are more than five seconds old, to avoid responding to 
    // cached measurements. 
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 
    if (locationAge > 5) return; 

    if (self.oldLocat == NULL) 
    {  
     // For the first acceptable measurement, simply set oldLocat and exit. 
     self.oldLocat = newLocation; 
     return; 
    } 

    // A second location has been identified. Calculate distance travelled. 
    // Do not set self.oldLocat from the oldLocation provided in this update, as we wish to 
    // use the last place we calculated distance from, not merely the last place that was 
    // provided in a location update. 
    CLLocationDistance distance = [newLocation distanceFromLocation:self.oldLocat]; 
    NSLog(@"Distance: %.0fm", distance); 

    // This new location is now our old location. 
    self.oldLocat = newLocation; 
} 

참고 : : :,하고 또한 속성 self.newLocat이 실제로 필요하지 않습니다에, 예를 들어

, 당신이 뭔가를 할 수 이 코드 섹션에서는