Development Tip

didUpdateToLocation 대신 didUpdateLocations

yourdevel 2020. 11. 12. 20:25
반응형

didUpdateToLocation 대신 didUpdateLocations


iOS6의 출시와 함께 Apple은 didUpdateToLocation 대신 didUpdateLocations를 사용하기를 원합니다. 누구든지 didUpdateLocations를 올바르게 사용하는 방법을 설명 할 수 있습니까?


마지막 위치를 얻기 위해 다음 대리자를 사용했다고 가정합니다.

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

위의 위임은 iOS 6에서 더 이상 사용되지 않습니다. 이제 다음을 사용해야합니다.

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

마지막 위치를 얻으려면 배열의 마지막 객체를 가져옵니다.

[locations lastObject]

즉, [locations lastObject](새 대리자)는 newLocation(이전 대리자) 와 같습니다 .


여기에 다른 답변 중 어디에도 위치 배열이있는 이유와 제공되는 새로운 didUpdateLocations : 배열을 올바르게 사용하는 방법이 설명되어 있지 않습니다.

locationManager:didUpdateToLocation:fromLocation:대신 위치의 NSArray를 사용하지 않고 보내는 목적은 백그라운드에서 실행할 때 전력 소비를 줄이는 것입니다.

iPhone 5부터 GPS 칩은 일정 시간 동안 위치를 저장 한 다음 한꺼번에 배열로 전달할 수있는 기능이 있습니다. 이를 지연된 위치 업데이트라고합니다. 이렇게하면 메인 CPU가 백그라운드에있는 동안 더 오랜 시간 동안 휴면 상태가됩니다. 이는 iOS가 모든 위치 업데이트를 위해 메인 CPU를 시작할 필요가 없다는 것을 의미하며, CPU는 잠자기 상태이며 GPS 칩은 위치를 수집합니다.

deferredLocationUpdatesAvailable방법 을 사용하여이 기능을 확인할 수 있습니다 . 가능한 경우 allowDeferredLocationUpdatesUntilTraveled:timeout:방법 을 사용하여 활성화 할 수 있습니다 . 일부 조건이 적용됩니다 . 자세한 내용 이 답변 을 참조하십시오.


사용할 수있는 마지막 위치에 액세스 할 수있는 객체 배열을 제공합니다.

[locations lastObject]

이 대리자 메서드에서

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

이것은 더 이상 사용되지 않는 것과 유사하게 작동하도록 메소드를 구현할 수있는 방법입니다.

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = locations.lastObject;
    CLLocation *oldLocation;
    if (locations.count > 1) {
        oldLocation = locations[locations.count - 2];
    }
}

iOS 5와 6을 모두 지원하는 경우

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

노인에서

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

기능, 위치 배열을 구축합니다.


locationManager : didUpdateLocations :에 의해 반환 된 CLLocation 객체의 배열은 이전 위치를 포함 할 수도 있고 포함하지 않을 수도 있습니다. 즉, 드물게 어레이에 위치가 하나만있을 수 있습니다. 다음을 사용하여 확인하고 둘 이상의 객체가있는 경우 가장 최근의 이전 위치를 검색 할 수 있습니다.

int objCount = [locations count];
if (objCount > 1) {
    CLLocation *oldLocation = [locations objectAtIndex:objCount - 1];
}

배열에 하나 이상의 객체가 있으므로 현재 위치 검색은 배열의 마지막 객체를 요청하여 수행됩니다. 마지막 개체가 유일한 개체 인 경우에도 괜찮습니다.

CLLocation *newLocation = [locations lastObject];

Keep in mind that because this is an array, oldLocation in the example shown above will not necessarily be the one you're looking for. This depends on how you setDistanceFilter: and desiredAccuracy: properties, as those properties will impact how your locations array is populated. The desired oldLocation may be buried deeper in the array.

참고URL : https://stackoverflow.com/questions/12602463/didupdatelocations-instead-of-didupdatetolocation

반응형