IOS 프로그래밍

본문 바로가기
사이트 내 전체검색


IOS 프로그래밍
IOS 프로그래밍

1. How To Get User Location in Xcode Swift

페이지 정보

작성자 관리자 댓글 0건 조회 4,078회 작성일 19-12-10 15:52

본문

1. How To Get User Location in Xcode Swift

 

​info.plist에

NSLocationWhenInUseUsageDescription

또는

NSLocationAlwaysUsageDescription

추가

GPS정보를 어플리케이션 사용중에만 사용한다면 NSLocationWhenInUseUsageDescription 에 안내 메세지를 입력한다.

GPS정보를 어플리케이션 항상 사용한다면 NSLocationAlwaysUsageDescription 에 안내 메세지를 입력한다.

"위치기반 컨텐츠 추천을 위해 GPS 정보가 필요합니다."

 

​import Foundation
import CoreLocation
import MapKit

class DiscoverViewController : UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var map: MKMapView!
    var locationManager: CLLocationManager!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        if (CLLocationManager.locationServicesEnabled())
        {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.startUpdatingLocation()
        }
    }

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

        let location = locations.last! as CLLocation

        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

        self.map.setRegion(region, animated: true)
    }
}

 

 

댓글목록

등록된 댓글이 없습니다.


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.