[iOS] Map Kit

r1verfuture·2021년 10월 14일
0

iOS

목록 보기
6/30

Map Kit

  • iOS 앱 개발 도중 Apple Map 을 사용하기 위해 필요하다.

Xcode Example

import MapKit // 이미 설치되어 있는 모듈이기 때문에 Cocoa Pod 으로 설치할 필요 없다.

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() // 마지막으로 위치가 업데이트되는 것을 멈추게 하는 기능
    }
}
profile
#iOS #Swift #Developer #Python

0개의 댓글