반응형
swift 2.0 -- mapview 사용하여 현재 위치 표시하기
환경 : swift 2.0, xcode 7.0, ios 9.0
참고 : https://www.youtube.com/watch?t=2&v=qrdIL44T6FQ
https://www.veasoftware.com/tutorials/2015/5/12/current-location-in-swift-xcode-63-ios-83-tutorial
유투브 동영상 보면서, mapview 사용하여 현재 위치 표시 따라하기
** info.plist 설정
-- 앱에서 위치정보 사용하기 위해서는 사용자 승인 얻어야 한다.
사용자 승인 얻기위해 info.plist 에 설정이 필요.
NSLocationAlwaysUsageDescription -- 항상 위치정보 사용
NSLocationWhenInUseUsageDescription -- 앱 사용중에만 위치정보 사용
** 소스
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import MapKit | |
import CoreLocation | |
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { | |
@IBOutlet weak var mapView: MKMapView! | |
let locationManager = CLLocationManager() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
self.locationManager.delegate = self | |
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest | |
self.locationManager.requestWhenInUseAuthorization() | |
self.locationManager.startUpdatingLocation() | |
self.mapView.showsUserLocation = true | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
// MARK: - Location Delegate Methods | |
// 새로운 위치정보 발생시 실행되는 메소드 | |
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
let location = locations.last | |
// 위치정보 반환 | |
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) | |
// MKCoordinateSpan -- 지도 scale | |
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta:0.01, longitudeDelta:0.01)) | |
self.mapView.setRegion(region, animated: true) | |
self.locationManager.stopUpdatingLocation() | |
} | |
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { | |
print("Errors: " + error.localizedDescription) | |
} | |
} | |
반응형
'swift, objective-C' 카테고리의 다른 글
Objective C -- 문법정리 : block (0) | 2016.03.21 |
---|---|
Objective C -- 문법정리 : 함수, 메소드 (0) | 2016.03.21 |
ios 9 -- http 사이트 접속 장애 해결하기 (0) | 2015.11.17 |
swift 2.0 -- 지도 위치 이동 추적하기 (0) | 2015.09.28 |
swift 2.0 -- parse 로그인 따라하기 (0) | 2015.09.22 |