https://may1coding.tistory.com/26
UIKit ;; Mapkit으로 지도에 현 위치 표시하기 2탄 (유저 현위치 띄우기)
https://may1coding.tistory.com/25 UIKit ;; Mapkit으로 지도에 현 위치 표시하기 1탄.. 내가 구현을 맡은 뷰!! 지도 하단의 버튼을 눌렀을 때 현재 위치를 지도에 띄워주고, 해당 위치를 하단에 표시해주는 작
may1coding.tistory.com
대망의 마지막..
앞서서 Mapkit을 이용해서 지도를 화면에 띄우고, 초기 위치(region)로 보여줄 부분을 설정하고,
유저의 현 위치를 지도에 띄웠다.
다음으로 할 일은
유저의 위치를 문자로 받아오는 것이다!
이를 위해 찾아보니
1. CLLocationManagerDelegate의 메서드인 didUpdateLocations에서 위도, 경도값을 얻어올 수 있고
2. CLGeocoder class를 통해 위도, 경도 값을 주소로 변경할 수 있다.
didUpdateLocations

해당 메서드는 새로운 위치 데이터가 생기면 알려준다.
이를 위해 우선 delegate를 설정 후 (ViewDidLoad 내부에 locationManager.delegate = self)
CLLocationManagerDelegate 프로토콜을 conform 해준다.
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
print(location.coordinate.latitude)
print(location.coordinate.longitude)
}
}
location 변화가 감지되면 locations.first에 값이 들어오고, 해당 값을 location으로 받아서
위도는 location.coordinate.latitude
경도는 location.coordinate.longitude로 알 수 있다.
위 경도를 print해보면 잘 나온다.
'iOS > UIKit' 카테고리의 다른 글
UIKit ;; hugging priority, compression resistance priority (+Intrinsic Size) (0) | 2023.03.10 |
---|---|
UIKit ;; UIStackView.Distribution (0) | 2023.03.10 |
UIKit ;; Mapkit으로 지도에 현 위치 표시하기 4탄 (위도, 경도 값을 주소로 변환하기) (0) | 2022.11.13 |
UIKit ;; Mapkit으로 지도에 현 위치 표시하기 2탄 (유저 현위치 띄우기) (0) | 2022.11.13 |
UIKit ;; Mapkit으로 지도에 현 위치 표시하기 1탄 (지도를 화면에 띄우고, 초기 위치 설정하기) (0) | 2022.11.13 |