Do it! 스위프트로 아이폰 앱 만들기
08장 맵 뷰로 지도 나타내기
세그먼트 컨트롤 배치하고 세그먼트 수와 타이틀 변경하기
Library(+) 에서 Map Kit View 를 찾아 배치하기
Label 2개 배치하기
맵 킷 뷰에 대한 아웃렛 변수 추가하기
import MapKit 추가하기
💡Map Kit: 지도 확대, 축소, 이동 등 지도 관련 기능 제공
레이블 2개에 대한 아웃렛 변수 추가하기
세그먼트 컨트롤에 대한 액션 함수 추가하기
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet var myMap: MKMapView!
@IBOutlet var lblLocationInfo1: UILabel!
@IBOutlet var lblLocationInfo2: UILabel!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
lblLocationInfo1.text = ""
lblLocationInfo2.text = ""
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
myMap.showsUserLocation = true
}
func goLocation(latitudeValue: CLLocationDegrees, longitudeValue: CLLocationDegrees, delta span: Double) {
let pLocation = CLLocationCoordinate2DMake(latitudeValue, longitudeValue)
let spanValue = MKCoordinateSpan(latitudeDelta: span, longitudeDelta: span)
let pRegion = MKCoordinateRegion(center: pLocation, span: spanValue)
myMap.setRegion(pRegion, animated: true)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let pLocation = locations.last
goLocation(latitudeValue: (pLocation?.coordinate.latitude)!, longitudeValue: (pLocation?.coordinate.longitude)!, delta: 0.01)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let pLocation = locations.last
goLocation(latitudeValue: (pLocation?.coordinate.latitude)!, longitudeValue: (pLocation?.coordinate.longitude)!, delta: 0.01)
//추가 코드
CLGeocoder().reverseGeocodeLocation(pLocation!, completionHandler: { (placemarks, error) in
let pm = placemarks!.first
let country = pm!.country
var address: String = country!
if pm!.locality != nil {
address += " "
address += pm!.locality!
}
if pm!.thoroughfare != nil {
address += " "
address += pm!.thoroughfare!
}
self.lblLocationInfo1.text = "현재 위치"
self.lblLocationInfo2.text = address
})
locationManager.stopUpdatingLocation()
}
func setAnnotation(latitudeValue: CLLocationDegrees, longitudeValue: CLLocationDegrees, delta span: Double, title strTitle: String, subtitle strSubtitle: String) {
let annotation = MKPointAnnotation()
annotation.coordinate = goLocation(latitudeValue: latitudeValue, longitudeValue: longitudeValue, delta: span)
annotation.title = strTitle
annotation.subtitle = strSubtitle
myMap.addAnnotation(annotation)
}
@IBAction func sgChangeLocation(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
self.lblLocationInfo1.text = ""
self.lblLocationInfo2.text = ""
locationManager.startUpdatingLocation()
}
else if sender.selectedSegmentIndex == 1 {
setAnnotation(latitudeValue: 37.555878, longitudeValue: 126.972295, delta: 0.01, title: "서울역", subtitle: "서울특별시 중구 소공동 세종대로18길 2")
self.lblLocationInfo1.text = "보고 계신 위치"
self.lblLocationInfo2.text = "서울역"
}
else {
setAnnotation(latitudeValue: 37.551848, longitudeValue: 127.073638, delta: 0.01, title: "세종대학교", subtitle: "서울특별시 광진구 능동로 209 세종대학교")
self.lblLocationInfo1.text = "보고 계신 위치"
self.lblLocationInfo2.text = "세종대학교"
}
}
import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet var myMap: MKMapView!
@IBOutlet var lblLocationInfo1: UILabel!
@IBOutlet var lblLocationInfo2: UILabel!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
lblLocationInfo1.text = ""
lblLocationInfo2.text = ""
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
myMap.showsUserLocation = true
}
func goLocation(latitudeValue: CLLocationDegrees, longitudeValue: CLLocationDegrees, delta span: Double) -> CLLocationCoordinate2D {
let pLocation = CLLocationCoordinate2DMake(latitudeValue, longitudeValue)
let spanValue = MKCoordinateSpan(latitudeDelta: span, longitudeDelta: span)
let pRegion = MKCoordinateRegion(center: pLocation, span: spanValue)
myMap.setRegion(pRegion, animated: true)
return pLocation
}
func setAnnotation(latitudeValue: CLLocationDegrees, longitudeValue: CLLocationDegrees, delta span: Double, title strTitle: String, subtitle strSubtitle: String) {
let annotation = MKPointAnnotation()
annotation.coordinate = goLocation(latitudeValue: latitudeValue, longitudeValue: longitudeValue, delta: span)
annotation.title = strTitle
annotation.subtitle = strSubtitle
myMap.addAnnotation(annotation)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let pLocation = locations.last
_ = goLocation(latitudeValue: (pLocation?.coordinate.latitude)!, longitudeValue: (pLocation?.coordinate.longitude)!, delta: 0.01)
CLGeocoder().reverseGeocodeLocation(pLocation!, completionHandler: { (placemarks, error) in
let pm = placemarks!.first
let country = pm!.country
var address: String = country!
if pm!.locality != nil {
address += " "
address += pm!.locality!
}
if pm!.thoroughfare != nil {
address += " "
address += pm!.thoroughfare!
}
self.lblLocationInfo1.text = "현재 위치"
self.lblLocationInfo2.text = address
})
locationManager.stopUpdatingLocation()
}
@IBAction func sgChangeLocation(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
self.lblLocationInfo1.text = ""
self.lblLocationInfo2.text = ""
locationManager.startUpdatingLocation()
}
else if sender.selectedSegmentIndex == 1 {
setAnnotation(latitudeValue: 37.555878, longitudeValue: 126.972295, delta: 0.01, title: "서울역", subtitle: "서울특별시 중구 소공동 세종대로18길 2")
self.lblLocationInfo1.text = "보고 계신 위치"
self.lblLocationInfo2.text = "서울역"
}
else {
setAnnotation(latitudeValue: 37.551848, longitudeValue: 127.073638, delta: 0.01, title: "세종대학교", subtitle: "서울특별시 광진구 능동로 209 세종대학교")
self.lblLocationInfo1.text = "보고 계신 위치"
self.lblLocationInfo2.text = "세종대학교"
}
}
}
locationManager.delegate = self
- delegate 설정을 하지 않으면 위치 업데이트 이벤트 처리, 위치 권한 요청, 위치 데이터 수집이 불가능하다.
locationManager(_ manager: didUpdateLocations locations: )
- 사용자의 현재 위치 업데이트를 감지하였을 때 실행된다.
미국 뭐에요 ㅋㅋ