본문 바로가기
ios 뽀개기/ios 응용해보기

맵뷰 map view

by 인생여희 2017. 12. 14.
반응형

맵뷰 map view



//세그먼트 버튼: 어떤 버튼이 선택되었는지 알 수 있다.



import UIKit

import MapKit //임포트


//부모 맵 클래스 상속

class ViewController: UIViewController,CLLocationManagerDelegate {


    let locationManger = CLLocationManager()

    

    @IBOutlet weak var mapKit: MKMapView!

    @IBOutlet weak var lable1: UILabel!

    @IBOutlet weak var lable2: UILabel!


    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        //위치정보 일단은 공백

        lable1.text = ""

        lable2.text = ""

        

        //델리게이트를 self로 지정

        locationManger.delegate = self

        //지도 정확도 최고

        locationManger.desiredAccuracy = kCLLocationAccuracyBest

        //사용자에게 승인 요구

        locationManger.requestWhenInUseAuthorization()

        //위치 업데이트

        locationManger.startUpdatingLocation()

        //위치보기 값

        mapKit.showsUserLocation = true

        

    }


    //지도를 나타내기 위한 함수 호출

    func goLocation(latitude latitudeValue: CLLocationDegrees, longtitude longtitudeValue: CLLocationDegrees, delta span: Double) -> CLLocationCoordinate2D{

        //위도와 경도값을 매개변수로 함수 호출

        let pLocation = CLLocationCoordinate2DMake(latitudeValue, longtitudeValue)

        //범위 값을 매개변수로 함수 호출

        let spanValue = MKCoordinateSpanMake(span, span)

        let pRegion = MKCoordinateRegionMake(pLocation, spanValue)

        mapKit.setRegion(pRegion, animated: true)

        

        return pLocation

    }


    //위치가 업데이트 되었을 때

    

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        //마지막 위치값 찾아내기

        let pLocation = locations.last

        //마지막 위치의 위도와 경도 값을 가지고 앞에서 만든 함수 호출  delta는 숫자가 작을 수록 확대됨

        goLocation(latitude: (pLocation?.coordinate.latitude)!, longtitude: (pLocation?.coordinate.longitude)!, delta: 0.01)

        

        

        CLGeocoder().reverseGeocodeLocation(pLocation!, completionHandler: {

            (placemarks,error) -> Void in

            let pm = placemarks!.first

            //나라값 대입

            let country = pm!.country

           //문자열 address 나라값 대입해주기

            var address:String = country!

            

            //지역 값이 존재하면 address에 대입

            if pm!.locality != nil {

                address += " "

                address += pm!.locality!

            }

            //도로값이 존재 하면 address에 대입

            if pm!.thoroughfare != nil {

                address += " "

                address += pm!.thoroughfare!

            }

            

            self.lable1.text = "현재위치"

            self.lable2.text = address

        })

        

        locationManger.stopUpdatingLocation()

        

    }

    

    

    

    

    ////특정 위도와 경도에 핀설치하고 핀에 타이틀과 서브타이틀 문자열 표시하기(위도, 경도, 범위, 타이틀, 서브타이틀)

    func setAnnotation(latitude latitudeValue: CLLocationDegrees, longtitude longtitudeValue : CLLocationDegrees, delta span: Double, title strTitle : String, subtitle strSubtitle : String){

        //핀을 설치하기 위한 함수 호출

        let annotation = MKPointAnnotation()

        annotation.coordinate = goLocation(latitude : latitudeValue, longtitude : longtitudeValue, delta : span)

        

        annotation.title = strTitle

        annotation.subtitle = strSubtitle

        mapKit.addAnnotation(annotation)

    }

    

    

    //세그먼트 컨트롤

    @IBAction func segment(_ sender: UISegmentedControl) {

        if sender.selectedSegmentIndex == 0 {

            self.lable1.text = ""

            self.lable2.text = ""

            //현재 위치 초기화

            locationManger.startUpdatingLocation()

            

        }else if(sender.selectedSegmentIndex == 1){


            setAnnotation(latitude: 37.5712897, longtitude: 126.98621430000003, delta: 0.01, title: "보고계신위치", subtitle: "회사")

            self.lable1.text = "보고계신위치"

            self.lable2.text = "회사"

            

        


        }else if(sender.selectedSegmentIndex == 2){

            setAnnotation(latitude: 37.5593582, longtitude: 126.95928759999993, delta: 0.01, title: "보고계신위치", subtitle: "울집")

            self.lable1.text = "보고계신위치"

            self.lable2.text = "우리집"

        }

        

    }

}




반응형

'ios 뽀개기 > ios 응용해보기' 카테고리의 다른 글

페이지 뷰  (0) 2017.12.15
탭뷰  (0) 2017.12.15
웹뷰 webview  (0) 2017.12.14
데이트 피커뷰와 alert이용해서 간단한 알람시계 구현  (0) 2017.12.14
두개의 피커 뷰 이용하기  (0) 2017.12.13

댓글