MkMapView 사용 방법(swift 버전)
제작된 응용 프로그램은 Github 위에 놓여 있다.
기능
다음과 같은 기능을 구현했습니다.
*지도에 현재 위치 표시 가능
*위도 경도를 레이블로 표시할 수 있음
* 현재 위치 추적 시작/종료 가능
*지도 확대/축소율 자동 조정 가능
*현재 위치에서 핀으로 고정 가능
*헤드 없이 리드 호출
프로그램 제작 절차
프로젝트 작성
참조여기. 프로젝트 작성
응용 프로그램에서 위치 정보 사용 설정
위치 정보 사용:.plist에 추가합니다.
* Privacy - Location When In Use Usage Description
* Privacy - Location Always Usage Description
이것은 프로그램을 처음 시작할 때 사용자에게 허가를 보낼 때의 정보입니다. // 位置情報取得時に、リクエストを出すと
locationManager!.requestWhenInUseAuthorization()
// このメソッドが呼ばれます
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
manager.requestWhenInUseAuthorization()
case .restricted, .denied:
break
case .authorizedAlways, .authorizedWhenInUse:
break
}
}
위치 정보 가져오기 처리 추가
위치 정보는 CLLocation Manager 클래스를 사용하여 얻을 수 있습니다.
개시하다
startUpdating Location을 실행합니다.
위치 정보가 업데이트되면 CLLocation Manager Delegate 프로토콜의 location Manager(CLLoction Manager, didUpdate Locations: [CLLoction])가 호출됩니다. @IBAction func tapStartButton(_ sender: UIButton) {
if locationManager != nil { return }
locationManager = CLLocationManager()
locationManager!.delegate = self
locationManager!.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager!.startUpdatingLocation()
}
// tracking user location
mapView.userTrackingMode = MKUserTrackingMode.followWithHeading
mapView.showsUserLocation = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let newLocation = locations.last else {
return
}
let location:CLLocationCoordinate2D
= CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude)
let latitude = "".appendingFormat("%.4f", location.latitude)
let longitude = "".appendingFormat("%.4f", location.longitude)
latLabel.text = "latitude: " + latitude
lngLabel.text = "longitude: " + longitude
// update annotation
mapView.removeAnnotations(mapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = newLocation.coordinate
mapView.addAnnotation(annotation)
mapView.selectAnnotation(annotation, animated: true)
// Showing annotation zooms the map automatically.
mapView.showAnnotations(mapView.annotations, animated: true)
}
끝맺다
stop Undating Location을 실행합니다. guard let manager = locationManager else { return }
manager.stopUpdatingLocation()
manager.delegate = nil
locationManager = nil
latLabel.text = "latitude: "
lngLabel.text = "longitude: "
// untracking user location
mapView.userTrackingMode = MKUserTrackingMode.none
mapView.showsUserLocation = false
mapView.removeAnnotations(mapView.annotations)
풋 뷰
핀은 MKPoint Annotation 클래스를 사용하는 것을 보여줍니다.
현재 위치를 하나만 표시하기 위해 모두 삭제하고 추가합니다. mapView.removeAnnotations(mapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = newLocation.coordinate
mapView.addAnnotation(annotation)
mapView.selectAnnotation(annotation, animated: true)
mapView.showAnnotations(mapView.annotations, animated: true)
시작 버튼 누르기 전
초기에는 일본이 전부였다.
모든 트랙을 표시합니다.
시작 단추를 눌렀을 때 맵뷰입니다.showAnnotations를 실행하면 모든 트랙이 표시됩니다.
mapView.setRegion을 사용하여 배율을 지정할 수 있습니다. var region:MKCoordinateRegion = mapView.region
region.center = location
region.span.latitudeDelta = 0.02
region.span.longitudeDelta = 0.02
mapView.setRegion(region,animated:true)
또한, 맵뷰.selectAnnotation으로 설정하면 매개변수로 전달된 핀의 호출이 표시됩니다.
참고 자료
// 位置情報取得時に、リクエストを出すと
locationManager!.requestWhenInUseAuthorization()
// このメソッドが呼ばれます
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
manager.requestWhenInUseAuthorization()
case .restricted, .denied:
break
case .authorizedAlways, .authorizedWhenInUse:
break
}
}
위치 정보는 CLLocation Manager 클래스를 사용하여 얻을 수 있습니다.
개시하다
startUpdating Location을 실행합니다.
위치 정보가 업데이트되면 CLLocation Manager Delegate 프로토콜의 location Manager(CLLoction Manager, didUpdate Locations: [CLLoction])가 호출됩니다.
@IBAction func tapStartButton(_ sender: UIButton) {
if locationManager != nil { return }
locationManager = CLLocationManager()
locationManager!.delegate = self
locationManager!.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager!.startUpdatingLocation()
}
// tracking user location
mapView.userTrackingMode = MKUserTrackingMode.followWithHeading
mapView.showsUserLocation = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let newLocation = locations.last else {
return
}
let location:CLLocationCoordinate2D
= CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude)
let latitude = "".appendingFormat("%.4f", location.latitude)
let longitude = "".appendingFormat("%.4f", location.longitude)
latLabel.text = "latitude: " + latitude
lngLabel.text = "longitude: " + longitude
// update annotation
mapView.removeAnnotations(mapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = newLocation.coordinate
mapView.addAnnotation(annotation)
mapView.selectAnnotation(annotation, animated: true)
// Showing annotation zooms the map automatically.
mapView.showAnnotations(mapView.annotations, animated: true)
}
끝맺다
stop Undating Location을 실행합니다.
guard let manager = locationManager else { return }
manager.stopUpdatingLocation()
manager.delegate = nil
locationManager = nil
latLabel.text = "latitude: "
lngLabel.text = "longitude: "
// untracking user location
mapView.userTrackingMode = MKUserTrackingMode.none
mapView.showsUserLocation = false
mapView.removeAnnotations(mapView.annotations)
풋 뷰
핀은 MKPoint Annotation 클래스를 사용하는 것을 보여줍니다.
현재 위치를 하나만 표시하기 위해 모두 삭제하고 추가합니다. mapView.removeAnnotations(mapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = newLocation.coordinate
mapView.addAnnotation(annotation)
mapView.selectAnnotation(annotation, animated: true)
mapView.showAnnotations(mapView.annotations, animated: true)
시작 버튼 누르기 전
초기에는 일본이 전부였다.
모든 트랙을 표시합니다.
시작 단추를 눌렀을 때 맵뷰입니다.showAnnotations를 실행하면 모든 트랙이 표시됩니다.
mapView.setRegion을 사용하여 배율을 지정할 수 있습니다. var region:MKCoordinateRegion = mapView.region
region.center = location
region.span.latitudeDelta = 0.02
region.span.longitudeDelta = 0.02
mapView.setRegion(region,animated:true)
또한, 맵뷰.selectAnnotation으로 설정하면 매개변수로 전달된 핀의 호출이 표시됩니다.
참고 자료
mapView.removeAnnotations(mapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = newLocation.coordinate
mapView.addAnnotation(annotation)
mapView.selectAnnotation(annotation, animated: true)
mapView.showAnnotations(mapView.annotations, animated: true)
var region:MKCoordinateRegion = mapView.region
region.center = location
region.span.latitudeDelta = 0.02
region.span.longitudeDelta = 0.02
mapView.setRegion(region,animated:true)
Reference
이 문제에 관하여(MkMapView 사용 방법(swift 버전)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/unokun/items/a5470feb6ee52d009dcc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)