본문 바로가기
Swift

mapView

by 루에 2015. 10. 30.
반응형

맵뷰는 MKMapView.framework을 사용한다.


기본적인 프로퍼티

// 확대, 축소, 스크롤 기능

var zoomEnabled : Bool

var scrollEnabled : Bool

var rotateEnabled : Bool

var pitchEnabled : Bool


// 지도 출력

var showsUserLocation : Bool

var showsPointsOfInterest : Bool

var showsBuildings : Bool

var showsTraffic : Bool    // iOS9


위치

// 지도가 표시하는 영역의 중앙 위치를 설정하거나 얻기

var centerCoordinate : CLLocationCoordinate2D

func setCenterCoordinate(coordinate: CLLocationCoordinate2D, animated: Bool)


// 지도가 표시하는 영역을 설정하거나 얻기

var region: MKCoordinateRegion

func setRegion(region: MKCoordinateRegion, animated: Bool)


위치정보는 위도와 경도로 구성된 CLLocationCoordinate2D 구조체를 사용한다. 구성은 아래와 같다.

struct CLLocationCoordinate2D {

var latitude: CLLocationDegrees

var longitude: CLLocationDegrees

init(latitude: CLLocationDegrees, longitude: CLLocationDegrees)

}


func CLLocationCoordinate2DMake(latitude: CLLocationDegrees, longitude: CLLocationDegrees) -> CLLocationCoordinate2D


영역을 지정하는 region은 MKCoordinateRegion 구조체로 정의되어 있다. 구조에는 중심위치 center와 맵뷰의 가로 세로 길이에 해당하는 위도, 경도의 차이에 해당하는 span으로 구성된다.

struct MKCoordinateSpan {

var latitudeDelta: CLLocationDegrees

var longitudeDelta: CLLocationDegrees

}


func MKCoordinateSpanMake(latitudeDelta: CLLocationDegrees, longitudeDelta: CLLocationDegrees) -> MKCoordinateSpan

func MKCoordinateRegionMake(centerCoordinate: CLLocationCoordinate2D, span: MKCoordinateSpan) -> MKCoordinateRegion


해운대가 맵뷰에 나타나도록 작성한 기본 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import UIKit
import MapKit
 
class ViewController: UIViewController {
 
    @IBOutlet weak var mapView: MKMapView!
    override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// var centerCoordinate : CLLocation
let center = CLLocationCoordinate2DMake(37.6, 127.8)
let span = MKCoordinateSpanMake(6.5, 6.5)
let region = MKCoordinateRegionMake(center, span)
// 맵뷰 표시영역 변경
mapView.setRegion(region, animated: true)
}
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
  
    @IBAction func btn1(sender: AnyObject) {
        let camera = mapView.camera.copy() as! MKMapCamera
        
        // 좌표
        camera.centerCoordinate = CLLocationCoordinate2DMake(35.16129.16)
        
        // 고도
        camera.altitude = 100
        // 각도
        camera.pitch = 0.0
        // 카메라 방향
        camera.heading = 0.0
        
        mapView.setCamera(camera, animated: true)
    }
    
    @IBAction func btn2(sender: AnyObject) {
    }
}
cs


반응형

댓글