MapKit으로 원하는 위치 띄우기

‍csk·2022년 11월 12일
0

Swift

목록 보기
3/6

Apple Documentetion -> MapKit

원하는 지역의 맵을 띄우기

private var mapView: MKMapView

mapView.setRegion(region: MKCoordinateRegion, animated: Bool)

MKCoordinateRegion(center: CLLocationCoordinate2D, span: MKCoordinateSpan)

특정 위도와 경도를 중심으로 하는 직사각형 지리적 지역.

  1. center로 지역의 중심 지점을 지정해줘야한다.
  2. span으로

1. Center 지정하기

var center: CLLocationCoordinate2D

1-1. CLLocationCoordinate2D

WGS 84 참조 프레임을 사용하여 지정된 위치와 관련된 위도와 경도.

CLLocationCoordinate2D(latitude: CLLocationDegrees, longitude: CLLocationDegrees)

1-2. latitude, longitude

var latitude: CLLocationDegrees
var longitude: CLLocationDegrees

CLLocationDegrees -> Double 타입이다.

var span: MKCoordinateSpan

2. Span 지정하기

표시할 지도의 양을 나타내는 수평 및 수직 스팬.

var span: MKCoordinateSpan

2-1. MKCoordinateSpan

지도 영역의 너비와 높이.
MKCoordinateSpan(latitudeDelta: CLLocationDegrees, longitudeDelta: CLLocationDegrees)

2-2. latitudeDelta, longitudeDelta

CLLocationDegrees -> Double 타입이다.

결론

import UIKit
import MapKit
import Then

    private var mapView: MKMapView = MKMapView().then {
        
        let location = CLLocationCoordinate2D(latitude: 37.49812745816443, longitude: 126.72430597470574)
        let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
        
        let region = MKCoordinateRegion(center: location, span: span)
        $0.setRegion(region, animated: true)
    }

결과

  • 너무 멀다 . span 을 줄여보자.

Span 수정후 결과

let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1) // before
let span = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005) // after 

profile
Developer

1개의 댓글

comment-user-thumbnail
2022년 12월 16일

감사합니다 유익하네요

답글 달기