dev._.note

[Swift] MapKit 지도 나타내기 본문

Dev/SWIFT

[Swift] MapKit 지도 나타내기

Laena 2024. 2. 26. 21:32

 

Info.plist(수정) : 앱이 사용자 위치에 접근하도록 허용하기

 

허용메시지 확인

 

import UIKit

import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate{
    @IBOutlet weak var myMap: MKMapView!
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        //상수 locationManager의 델리게이트를 self로 설정
        locationManager.delegate = self

        //정확도를 최고로 설정
        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        //위치 데이터를 추적하기 위해 사용자에게 승인을 요구
        locationManager.requestWhenInUseAuthorization()

        //위치 업데이트를 시작
        locationManager.startUpdatingLocation()

        //위치보기 값을 true로 설정
        myMap.showsUserLocation = true

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}