IOS
IOS 위도 및 경도 받아오기 및 위치권한 팝업 띄우기CLLocationManagerDelegate
일용직 코딩노동자
2020. 5. 15. 13:10
728x90
반응형
import CoreLocation
lazy var locationManager = CLLocationManager()
var Location_Address: CLLocation?
var lastMyLocation: CLLocation?
이런 식으로 위치관련 변수 및 셋팅을 해줍니다.
클래스딴 전역으로 만들어주세요.
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.startUpdatingLocation()
그리고 위치권한 팝업을 띄워줍니다.
extension ViewController: CLLocationManagerDelegate {
//위도와 경도를 받아오는 코드
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
guard let location = locations.last else {
return
}
Location_Address = location
print("위도 : ",lastMyLocation?.coordinate.latitude," 경도 : ",lastMyLocation?.coordinate.longitude)
}
}
extension 으로 분리 시켜주신 후, CLLocationManagerDelegate 프로토콜을 포함시켜줍니다.
Location_Address에 위도와 경도가 주기별로 리셋이 됩니다.
받아오는 것은 print문에 찍힌것처럼 위도와 경도를 분리해서 받아오시면 됩니다.
728x90
반응형