1. How To Get User Location in Xcode Swift
페이지 정보
작성자 관리자 댓글 0건 조회 4,849회 작성일 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)
}
}
댓글목록
등록된 댓글이 없습니다.