본문 바로가기
아이폰 개발/ios 개념&튜토리얼

iOS CLLocation 위도경도, 속도, 이동거리 구하기

by 인생여희 2021. 2. 18.

iOS CLLocation 위도경도속도이동거리 구하기

 

#.아래 포스팅 이어서 작성

iOS CoreLocation 공통 모듈 - 싱글톤

 

 

 

[STEP 1]  이전위치, 새 위치, 거리 , 속도를 위한 변수 추가

 

@property (nonatomic , strong) CLLocation *oldLocation;

@property (nonatomic , strong) CLLocation *mynewLocation;

@property (nonatomic , strong) NSString *distance;

@property (nonatomic, strong) NSMutableArray *cLLocationArray;

@property double speed;

 

[STEP 2]  위치값 저장을 위한   배열 초기화

 

_cLLocationArray = [[NSMutableArray alloc]init];

 

 

[STEP 3] 위치 업데이트 델리게이트 메소드 작성

 

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

    

    NSLog(@"위치 수집 업데이트 - didUpdateLocations");

    

    //1.이전 위치, 새 위치를 위한 배열 개수

    NSUInteger objCount = [_cLLocationArray count];

    NSLog(@"locations.count = %lu", (unsigned long)objCount ) ;

    

    

    //2.속도

    _speed = [[locations lastObject] speed];

    

    //멈춰 있을때 -1로 들어온다.

    if (_speed < 0) {

        _speed = 0;

    }

    

    // m/s      km/h 로 번경

    //_speed = _speed / 1000 * 3600;

   

    NSLog(@"speed - speed : %.2f" , [[locations lastObject]speed] );

    

    

    //3.최종 위치 삽입

    [_cLLocationArray addObject:[locations lastObject]];

    

    //4.이전 위치

    _oldLocation = nil;

    if (objCount > 1) {

        _oldLocation = [_cLLocationArray objectAtIndex:objCount - 1];

    }

    

    //5.새로운 위치

    _mynewLocation = [_cLLocationArray lastObject];

    

    NSLog(@"oldLocation - latitude : %f" , _oldLocation.coordinate.latitude);

    NSLog(@"oldLocation - longitude : %f" , _oldLocation.coordinate.longitude);

    NSLog(@"newLocation - latitude : %f" , _mynewLocation.coordinate.latitude);

    NSLog(@"newLocation - longitude : %f" , _mynewLocation.coordinate.longitude);

    

    //6.거리

    CLLocationDistance between = [_mynewLocation distanceFromLocation:_oldLocation];

    _distance = [NSString stringWithFormat:@"%.2f" , between];

    



   //7.배열이 일정 크기 이상이면 삭제해주기

    if (objCount > 5) {

        [_cLLocationArray removeObjectAtIndex:0];

    }

    

}

 

 

[STEP 4]  사용하기

  

//1.이전 위치

    CLLocationDegrees oldlatitude =

       [CoreLocation sharedSingleton].oldLocation.coordinate.latitude;

    

    CLLocationDegrees oldlongitude =

    [CoreLocation sharedSingleton].oldLocation.coordinate.longitude;

    

    

//2.신규 위치

    CLLocationDegrees newlatitude =

    [CoreLocation sharedSingleton].mynewLocation.coordinate.latitude;

    

    CLLocationDegrees newlongitude =

    [CoreLocation sharedSingleton].mynewLocation.coordinate.longitude;

    

    

//3.위도, 경도, 거리 , 속도

    NSString *result = [NSString stringWithFormat:@"이전위치 : 위도(lat) : %f , 경도(long) : %f \n" ,oldlatitude ,oldlongitude];

    

    result = [result stringByAppendingFormat:@"새 위치 : 위도(lat) : %f , 경도(long) : %f \n" ,newlatitude ,newlongitude];

    

    result = [result stringByAppendingFormat:@"거리 : %@ \n" , [[CoreLocation sharedSingleton]distance]];

    

    result = [result stringByAppendingFormat:@"속도 : %f \n" , [[CoreLocation sharedSingleton]speed]];

    

    result = [result stringByAppendingFormat:@"----------------- \n"];