수색…


거리 필터 사용

예 :

   CLLocationManager *locationManager = [[CLLocationManager alloc] init];
   locationManager.delegate = self;
   locationManager.desiredAccuracy = kCLLocationAccuracyBest;
   locationManager.distanceFilter = 5;

예 위의 위 코드 예에서 5m 미만의 위치 변경은 콜백으로 보내지지 않고 대신 무시됩니다.

CLLocationManager를 사용하여 사용자 위치 가져 오기

1 - 프로젝트에 CoreLocation.framework를 포함하십시오. 이 작업은 다음을 클릭하여 수행됩니다.

root directory -> build phases -> Link Binary With Libraries

(+) 버튼을 클릭하고 CoreLocation.framework를 찾은 다음 추가를 클릭하십시오.

2 info.plist 파일을 수정하여 소스 코드로 열어 사용자 위치 사용 권한을 요청합니다. 다음 키 중 하나를 추가하십시오. 응용 프로그램을 사용하는 동안 사용자 위치 사용을 요청하는 태그 아래에 value 쌍 :

<key>NSLocationWhenInUseUsageDescription</key>
<string>message to display when asking for permission</string>

3 CoreLocation을 사용하는 ViewController로 가져옵니다.

import CoreLocation

4- ViewController가 CLLocationManagerDelagate 프로토콜을 준수하는지 확인하십시오.

class ViewController: UIViewController,CLLocationManagerDelegate {}

이 단계가 끝나면 CLLocationManager 객체를 인스턴스 변수로 만들어 ViewController에서 사용할 수 있습니다.

var manager:CLLocationManager!

관리자가 위임자, 업데이트 간격 이전의 최소 거리 및 정확성을 지정하도록 관리자를 수정하기 때문에 여기서는 '사용'을 사용하지 않습니다.

//initialize the manager
manager = CLLocationManager()

//specify delegate
manager.delegate = self

//set the minimum distance the phone needs to move before an update event is triggered (for example:  100 meters)
manager.distanceFilter = 100

//set Accuracy to any of the following depending on your use case

//let kCLLocationAccuracyBestForNavigation: CLLocationAccuracy
//let kCLLocationAccuracyBest: CLLocationAccuracy
//let kCLLocationAccuracyNearestTenMeters: CLLocationAccuracy
//let kCLLocationAccuracyHundredMeters: CLLocationAccuracy
//let kCLLocationAccuracyKilometer: CLLocationAccuracy
//let kCLLocationAccuracyThreeKilometers: CLLocationAccuracy

manager.desiredAccuracy = kCLLocationAccuracyBest

//ask the user for permission
manager.requestWhenInUseAuthorization()

//Start collecting location information
if #available(iOS 9.0, *) {
            
   manager.requestLocation()
            
 } else {
  
   manager.startUpdatingLocation()
  
  }

이제 위치 업데이트에 액세스하려면 distanceFilter에 도달하면 초과 근무라는 기능을 구현할 수 있습니다.

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {}

위치 매개 변수는 장치의 실제 위치를 나타내는 CLLocation 객체의 배열입니다. 이러한 객체에서 coordinate,altitude, floor, horizontalAccuracy, verticalAccuracy, timestamp, description, course, speed 정확도, 수직 정확도 coordinate,altitude, floor, horizontalAccuracy, verticalAccuracy, timestamp, description, course, speed 및 coordinate,altitude, floor, horizontalAccuracy, verticalAccuracy, timestamp, description, course, speed 속성에 액세스 할 수 있습니다. 두 위치 간의 거리를 측정하는 함수 distance(from:) 입니다.

참고 : 위치에 대한 권한을 요청하는 동안 두 가지 유형의 승인이 있습니다.

'사용 중일 때'권한 부여는 앱이 사용 중이거나 포 그라운드 일 때 내 위치를 수신 할 권한 만 부여합니다.

'항상'권한 부여는 앱을 폐쇄 한 경우 배터리 수명을 단축시킬 수있는 앱 사용 권한을 제공합니다.

Plist 파일은 필요에 따라 조정해야합니다.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow