사용환경
* Swift4.x
* Xcode 9.3
사용권한 설정
* Info.plist 파일 내 Privacy Set
-
Privacy - Location Always and When In Use Usage Description —> 얘는 뭔지 모르겠다..;
-
Privacy - Location Always Usage Description —> GPS 정보를 항상 사용할때
-
Privacy - Location When In Use Usage Description —> GPS 정보를 앱 사용 중에만 사용할때
위의 권한들 중 원하는 권한은 선언해주면 된다.
FrameWork 추가
프로젝트의 Linked Frameworks and Libraries에서 CoreLocation.framework를 추가한다.
CLLocationManager
DesiredAccuracy - 센서의 정밀도
● kCLLocationAccuracyBestForNavigation;
● kCLLocationAccuracyBest;
● kCLLocationAccuracyNearestTenMeters;
● kCLLocationAccuracyHundredMeters;
● kCLLocationAccuracyKilometer;
● kCLLocationAccuracyThreeKilometers;
각각의 거리 등으로 위치 정보를 수신하도록 설정할 수 있다.
@available(iOS 8.0, *)
open func requestWhenInUseAuthorization() //위치 정보를 요청할때 권한을 요청한다.
open func startUpdatingLocation() // 위치 정보 수신을 시작한다.
open func stopUpdatingLocation() // 위치 정보 수신을 중단한다.
@available(iOS 9.0, *)
open func requestLocation() //위치 정보 수신을 요청한다.
CLLocationManagerDelegate
@available(iOS 6.0, *)
optional public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
//위치 정보가 업데이트 될때마다 호출된다.
@available(iOS 2.0, *)
optional public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
//위치 정보 수신 실패 시 호출된다.
@available(iOS 4.2, *)
optional public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
//위치 정보 사용 권한이 바뀔 때 호출된다.
let locationManager : CLLocationManager = CLLocationManager()
var location : CLLocationCoordinate2D? = CLLocationCoordinate2D()
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
if let location = self.locationManager.location { self.location = location.coordinate }
extension ViewController : CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
self.locationManager.stopUpdatingLocation()
self.location = location.coordinate
let findLocation = CLLocation(latitude: (self.location?.latitude)!, longitude: (self.location?.longitude)!)
let geocoder = CLGeocoder()
let locale = Locale(identifier: "ko_KR")
if #available(iOSApplicationExtension 11.0, *) {
geocoder.reverseGeocodeLocation(findLocation, preferredLocale: locale) { (place, error) in
if let address: [CLPlacemark] = place {
if let name = address.last?.name { print(name) }
}
}
} else {
geocoder.reverseGeocodeLocation(findLocation) { (place, error) in
if let address: [CLPlacemark] = place {
if let name = address.last?.name { print(name) }
}
}
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { print("location fail \(error)") }
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
var shouldAllow = false
switch status {
case .denied:
print("denied")
case .restricted:
print("restricted")
case .notDetermined:
print("notDetermined")
default:
shouldAllow = true
}
if shouldAllow {
self.locationManager.requestLocation()
}
}
}
위치 정보에 따른 주소 출력하기
let findLocation = CLLocation(latitude: (self.location?.latitude)!, longitude: (self.location?.longitude)!)
let geocoder = CLGeocoder()
let locale = Locale(identifier: "ko_KR") //원하는 지역 코드를 입력하면 된다.
if #available(iOSApplicationExtension 11.0, *) {
geocoder.reverseGeocodeLocation(findLocation, preferredLocale: locale) { (place, error) in
if let address: [CLPlacemark] = place {
if let name = address.last?.name { print(name) }
}
}
} else {
geocoder.reverseGeocodeLocation(findLocation) { (place, error) in
if let address: [CLPlacemark] = place {
if let name = address.last?.name { print(name) }
}
}
}