2014-03-03 1 views
0

iOS 앱에서 출발점에서 현재 위치까지의 거리를 추적해야합니다. 나는이 코드를 구현했다 :두 지점 사이의 거리를 가져 오는 중 문제가 발생했습니다.

그러나 나는이 앱을 사용하려고 할 때 거리가 없다. 때문에, 나는 그것이 정확하지 있다고 알고 있지만, 내가 가속도계를 사용할 수 없습니다

steps = distance/length of human step

: 나는 라벨에 표시하고 거리에 나가이 방정식을 사용하여 단계의 수를 계산합니다 거리가 필요합니다 iPhone의 디스플레이가 활성화되어 있지 않으면 작동하지 않습니다. 한 사람이 나를 제안했습니다 this solution. 왜 내 코드가 내게 거리를 제공하지 않습니까?

+1

을 무엇을하고 있는지. 거리를 얻으려고하면 거리의 가치는 무엇입니까? currentLocation 및 startLocation의 가치는 무엇입니까? 우리가하는 일에 대한 정보를주지 않으면, 우리는별로 도움이되지 못합니다. "그것은 작동하지 않습니다"매우 설명 적이 지 않습니다. – Fogmeister

답변

2

콜백

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 

당신에게 적어도 하나 개의 위치를 ​​제공 않으며, 위치 업데이트 이전에 연기 된 경우 위치 배열은 sonly 여러 개체 누르고 있습니다. 도보/주행 거리를 얻으려면 클래스 변수 또는 속성에 초기 위치를 저장해야합니다. 그런 다음 거리를 계산하려면 위의 코드에서와 같이하지만 초기 위치를 보유하는 클래스 변수를 사용하십시오. 점점 거리 코드 아래

+0

감사합니다, 나는 바보 같은 실수를 이해합니다. 그래서이 코드에 의해 시작 위치를 속성에 저장해야합니다'self.startLocation = [locations firstObject]; 그리고이 코드에 의해 거리를 계산해야만합니다'CLLocationDistance distance = [self.startLocation distanceFromLocation : currentLocation]; 권리? – lucgian841

+2

@ lucgian841 : startPosition과 currentPosition 사이의 거리를 정의 할 수 없다면 "startPosition에서 얼마나 멀리 떨어져 있는가"가 아니라 "실행 시간"을 찾고있는 경우를 제외하고는 다음을 합산해야합니다. pos1에서 pos2 + pos2에서 pos3 + ... + posx에서 currentPosition까지. 그렇지 않은 경우 사용자가 50km를 달리다가 다시 49km를 주행하면 거리는 99km가 아닌 1km가됩니다. 이 도움을 호프 –

+0

네, 또한 @ArmandDOHM에 의한 코멘트를 참고하시기 바랍니다 – Volker

0

확인 : 그것은

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 


    if ([coordArray count]>0) { 

     CLLocation *currentLocation = manager.location; 
     CLLocation *startLocation = [coordArray objectAtIndex:0]; 
     [coordArray addObject:currentLocation]; 
     float speed = currentLocation.speed * 3.6; 
     if (speed > 0) { 
      NSLog(@"\n speed:%@",[NSString stringWithFormat:@"%.2f Km/h", speed]); 
     } 

     CLLocationDistance distance = [startLocation distanceFromLocation:currentLocation]; 

     if (distance>0) { 
      NSLog(@"Distance:%@",[NSString stringWithFormat:@"%lf meters",distance]); 
     } 

    } 
    else{ 
     [coordArray addObject:manager.location]; 
    } 
}