swift

Swift 지도 만들기 Map View

이나주니 2024. 11. 23. 14:48
반응형
//
//  ViewController.swift
//  Map
//
//  Created by 인하준 on 11/23/24.
//

import UIKit
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet var lblLocationInfo1: UILabel!
    @IBOutlet var lblLocationInfo2: UILabel!
    @IBOutlet var myMap: MKMapView!
    
    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 // 위치값 보여주시 설정 true
    }
    
    // 위도 경도로 원하는 위치 표시
    func goLocation(latitudValue: CLLocationDegrees, longitudeValue: CLLocationDegrees, delta span : Double) -> CLLocationCoordinate2D {
        let pLocation = CLLocationCoordinate2DMake(latitudValue, longitudeValue)
        let spanValue = MKCoordinateSpan(latitudeDelta: span, longitudeDelta: span)
        let pRegion = MKCoordinateRegion(center: pLocation, span: spanValue)
        myMap.setRegion(pRegion, animated: true)
        return pLocation
    }
    
    // 위치 정보 추출후 텍스트 표시
    func locationManager(_ manager: CLLocationManager, didUpdateLocations location: [CLLocation]) {
        
        let pLocation = location.last
        _ = goLocation(latitudValue: (pLocation?.coordinate.latitude)!, longitudeValue: (pLocation?.coordinate.longitude)!, delta: 0.01) // 0.01 지도 확대
        
        CLGeocoder().reverseGeocodeLocation(pLocation!, completionHandler: {
            (CLPlacemark, error) -> Void in
            let pm = CLPlacemark!.first
            let contury = pm!.country
            var address: String = contury!
            if pm!.locality != nil {
                address += " "
                address += pm!.locality!
            }
            
            if pm!.thoroughfare != nil {
                address += " "
                address += pm!.thoroughfare!
            }
            
            self.lblLocationInfo1.text = "현재위치"
            self.lblLocationInfo2.text = address
        })
        
        // 마지막 위치 업데이트
        // 작성이의 경우 시뮬레이터 -> 상단 Features -> Location -> Custom Location 에서 위도 경보 입력해 현재 위치 고정
        locationManager.stopUpdatingLocation()
    }
    
    // 핀 설정
    func setAnnotation(latitudeValue: CLLocationDegrees, longitudeValue: CLLocationDegrees , delta span : Double, title strTitle: String, subTitle strSubtitle: String) {
        let annotaion = MKPointAnnotation()
        annotaion.coordinate = goLocation(latitudValue: latitudeValue, longitudeValue: longitudeValue, delta: span)
        annotaion.title = strTitle
        annotaion.subtitle = strSubtitle
        myMap.addAnnotation(annotaion)
    }
    
    // 상단 탭 선택
    @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.54290418033837, longitudeValue: 126.95270146392042, delta: 0.01, title: "우리 회사", subTitle: "공덕이야")
            self.lblLocationInfo1.text = "보고 있는 곳"
            self.lblLocationInfo2.text = "우리 회사"
        } else if sender.selectedSegmentIndex == 2 {
            // 서울 시청
            setAnnotation(latitudeValue: 37.56675213526838, longitudeValue: 126.97872019366427, delta: 0.01, title: "서울 시청", subTitle: "서울 시청임")
            self.lblLocationInfo1.text = "보고 있는 곳"
            self.lblLocationInfo2.text = "서울 시청"
            
        }
    }
    
}

 

 

 

 

 

반응형