개발자 코드(Code)/Swfit

IOS) PageControl + Map (PageControl + ImageView + Label)

Chain X 2020. 8. 30. 19:12
728x90
반응형

01

//
//  ViewController.swift
//  PageControlMap
//
//  Created by Leo_Jang on 27/08/2020.
//  Copyright © 2020 Leo_Jang. All rights reserved.
//

import UIKit
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var lblName: UILabel!
    @IBOutlet weak var myMap: MKMapView!
    @IBOutlet weak var pageChange: UIPageControl!
    
                    // 현재 위치는 CLLocationManager 가 알려준다.
    let locationManager = CLLocationManager()
    var kLocation = ["혜화문":( 37.5878892 , 127.00037098),"흥인지문":(37.5711907 , 127.009506),"창의문":(37.5926027,126.9664771),"숙정문":(37.5956584 , 126.9810576)]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        lblName.text = ""
        locationManager.delegate = self
        
        //정확도를 최고도로 설정
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        
        // 위치 데이터를 확인하기 위해 승인 요청
        locationManager.requestWhenInUseAuthorization()
        
        // 위치 업데이트 시작
        locationManager.startUpdatingLocation()
        
        // 위치 업데이트 시작
        myMap.showsUserLocation = true
        
        // PageControl 초기화면 설정
        pageChange.numberOfPages = 4
        pageChange.currentPage = 0
        pageChange.pageIndicatorTintColor = UIColor.green
        pageChange.currentPageIndicatorTintColor = UIColor.blue
        
        // 핀으로 첫화면을 설정해준다.
        setAnnotation(latitudeValue: kLocation["혜화문"]!.0, longtitudeValue: kLocation["혜화문"]!.1, delta: 0.01, title: "혜화문", subTitle: "서울도성길")
    }
    
    // 원하는 위도와 경도의 지도 띄우기
    // 위도와 경도에 대한 함수
    // 위도와 경도에 대한 함수 (delta 값으로 확대 / 축소)
    func goLocation(latitudeValue: CLLocationDegrees, longtitudeValue: CLLocationDegrees, delta span: Double)->CLLocationCoordinate2D{
        
        let pLocation = CLLocationCoordinate2DMake(latitudeValue, longtitudeValue) // 이 좌표값은 GPS가 가져온다.
        let spanValue = MKCoordinateSpan(latitudeDelta: span, longitudeDelta: span) // 가로세로 크기를 동일하게 키우겠다. span은 확장
        let pRegion = MKCoordinateRegion(center: pLocation, span: spanValue)
        myMap.setRegion(pRegion, animated: true)
        return pLocation
    }

    // pin 설치
    func setAnnotation(latitudeValue:CLLocationDegrees, longtitudeValue: CLLocationDegrees, delta span:Double, title strTitle: String, subTitle strSubTitle:String){
        // pin
        let annotation = MKPointAnnotation()
        // 어느 위치에 찍을 건지 알려주는 것
        annotation.coordinate = goLocation(latitudeValue: latitudeValue, longtitudeValue: longtitudeValue, delta: span)
        // 타이틀, 서브타이틀
        annotation.title = strTitle
        annotation.subtitle = strSubTitle
        myMap.addAnnotation(annotation)
    }
    
    @IBAction func mapChange(_ sender: UIPageControl) {
        self.lblName.text = "한양 도성길"
        switch pageChange.currentPage{
        case 0:
            setAnnotation(latitudeValue: kLocation["혜화문"]!.0, longtitudeValue: kLocation["혜화문"]!.1, delta: 0.01, title: "혜화문", subTitle: "한양 도성길")
        case 1:
            setAnnotation(latitudeValue: kLocation["흥인지문"]!.0, longtitudeValue: kLocation["흥인지문"]!.1, delta: 0.01, title: "흥인지문", subTitle: "한양 도성길")
        case 2:
            setAnnotation(latitudeValue: kLocation["창의문"]!.0, longtitudeValue: kLocation["창의문"]!.1, delta: 0.01, title: "창의문", subTitle: "한양 도성길")
        case 3:
            setAnnotation(latitudeValue: kLocation["숙정문"]!.0, longtitudeValue: kLocation["숙정문"]!.1, delta: 0.01, title: "숙정문", subTitle: "한양 도성길")
        default:
            locationManager.startUpdatingLocation()
        }
     }
} // -------

반응형