Map Kit
- iOS 앱 개발 도중 Apple Map 을 사용하기 위해 필요하다.
Xcode Example
import MapKit
class ViewController : UIViewController {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var latitude : Int = 0
var longitude : Int = 0
var locationName : String = ""
var locationAddress : String = ""
override func viewDidLoad() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
setAnnotation(latitudeValue: latitude, longitudeValue: longitude, delta: 0.0005, title: locationName, subtitle: locationAddress)
}
}
extension ViewController: CLLocationManagerDelegate {
func goLocation(latitudeValue: CLLocationDegrees, longitudeValue: CLLocationDegrees, delta span: Double) -> CLLocationCoordinate2D {
let pLocation = CLLocationCoordinate2DMake(latitudeValue, longitudeValue)
let spanValue = MKCoordinateSpan(latitudeDelta: span, longitudeDelta: span)
let pRegion = MKCoordinateRegion(center: pLocation, span: spanValue)
mapView.setRegion(pRegion, animated: true)
return pLocation
}
func setAnnotation(latitudeValue: CLLocationDegrees, longitudeValue: CLLocationDegrees, delta span: Double, title strTitle: String, subtitle strSubtitle: String) {
let annotation = MKPointAnnotation()
annotation.coordinate = goLocation(latitudeValue: latitudeValue, longitudeValue: longitudeValue, delta: span)
annotation.title = strTitle
annotation.subtitle = strSubtitle
mapView.addAnnotation(annotation)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let pLocation = locations.last
_ = goLocation(latitudeValue: (pLocation?.coordinate.latitude)!, longitudeValue: (pLocation?.coordinate.longitude)!, delta: 0.01)
locationManager.stopUpdatingLocation()
}
}