개발자 코드(Code)/Swfit

IOS) GPS 위치 (사진필요)

Chain X 2020. 8. 27. 08:39
728x90
반응형
//
//  ViewController.swift
//  Map
//
//  Created by Leo_Jang on 26/08/2020.
//  Copyright © 2020 Leo_Jang. All rights reserved.
//

import UIKit
import MapKit  /// **************** 반드시 써야 합니다.

class ViewController: UIViewController, CLLocationManagerDelegate {
    //CLLocationManagerDelegate < 반드시 입력하세요.
    
    @IBOutlet weak var lblLocationInfo1: UILabel!
    @IBOutlet weak var lblLocationInfo2: UILabel!
    @IBOutlet weak 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
     }
     
     // 원하는 위도와 경도의 지도 띄우기
     // 위도와 경도에 대한 함수
     func goLication(latitudeValue: CLLocationDegrees, longitudeValue: CLLocationDegrees, delta span: Double) ->CLLocationCoordinate2D {
         
         let pLocation = CLLocationCoordinate2DMake(latitudeValue, longitudeValue) // 이 좌표값은 GPS가 가져온다.
         let spanValue = MKCoordinateSpan(latitudeDelta: span, longitudeDelta: span) // 가로세로 크기를 동일하게 키우겠다. span은 확장
         let pRegion = MKCoordinateRegion(center: pLocation, span: spanValue)
         myMap.setRegion(pRegion, animated: true)
         return pLocation
     }
     // 위치가 업데이트 되었을때 지도의 위치를 표시해주는 함수
        func locationManager(_ manager:CLLocationManager, didUpdateLocations locations: [CLLocation]){
            let pLocation = locations.last
            _ = goLication(latitudeValue: (pLocation?.coordinate.latitude)!, longitudeValue: (pLocation?.coordinate.longitude)!, delta: 0.01) // delta : 0.01 -> 기존 지도의 100배 확대
            
            CLGeocoder().reverseGeocodeLocation(pLocation!, completionHandler:  {(placemarks, error)-> Void 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()
        }
        // Pin 설치
        func setAnnotation(latitudeValue:CLLocationDegrees, longitudeValue : CLLocationDegrees, delta span: Double, title strTitle: String, subTitle strSubTitle:String){
            
            let annotation = MKPointAnnotation()
            annotation.coordinate = goLication(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.65243153, longitudeValue: 127.0276397, delta: 0.01, title: "둘리 뮤지엄", subTitle: "서울특별시 도봉구 시루봉로 1길 6")
                self.lblLocationInfo1.text = "보고 계신 위치"
                self.lblLocationInfo2.text = "둘리 뮤지엄"
            }else if sender.selectedSegmentIndex == 2 {
                // 서대문 형무소 역사관
                setAnnotation(latitudeValue: 37.57244171, longitudeValue: 126.9595412, delta: 0.01, title: "서대문형무소 역사관", subTitle: "서울특별시 서대문구 통일로 251")
                self.lblLocationInfo1.text = "보고 계신 위치"
                self.lblLocationInfo2.text = "서대문 형무소 역사관"
            }
            
        }
        

    }

 

반응형