본문 바로가기
Swift

맵뷰에 어노테이션 추가/삭제

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

맵뷰에서 어노테이션이란, 핀을 꽃아 장소를 설정하는 것을 말한다.


MKPointAnnotation() 을 이용해서 장소를 설정하고 제거한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14

 @IBAction func btn2(sender: AnyObject) {

        // 어노테이션 설정

        let point = MKPointAnnotation()

        point.coordinate = CLLocationCoordinate2DMake(35.1598, 129.1626)

        point.title = "해운대"

        point.subtitle = "부산"

        mapView.addAnnotation(point)

    }

    

    @IBAction func removeAnnotation(sender: AnyObject) {

        // 어노테이션 전체 삭제

        mapView.removeAnnotations(mapView.annotations)

    }

 
cs



앱은 이런식으로 실행





커스텀 어노테이션


기본적으로 제공되는 어노테이션으로 충분한 데이터를 다루기 어렵기 때문에 MKPointAnnotation을 상속한 클래스를 생성해 커스텀 어노테이션을 만들고, 종류별로 추가할 수 있다.

MKMapViewDelegate 프로토콜을 이용해 테이블 부에 리스트를 추가하듯이 추가한다. 추가하는 메소드는

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView

이다.


아래 코드를 참조하자.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import UIKit
import MapKit
 
class CustomAnnotation: MKPointAnnotation {
    // 기본 어노테이션에 url정보 추가
    var url : String?
}
class ViewController: UIViewController,MKMapViewDelegate {
 
    @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  func moveToSeoul() {
        let center = CLLocationCoordinate2DMake(37.551403126.988045)
        let span = MKCoordinateSpanMake(0.20.2)
        let region = MKCoordinateRegionMake(center, span)
        mapView.setRegion(region, animated: true)
    }
    
    override func viewWillAppear(animated: Bool) {
        addAnnotation()
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func addAnnotation(){
        // 씨네큐브 : 37.56978,126.972132
        mapView.addAnnotation(createAnnotation("씨네큐브", coordinate: CLLocationCoordinate2DMake(37.56978,126.972132), exhibitions: nil, url: nil))
        // 인디스페이스 : 37.570653,126.97164
        mapView.addAnnotation(createAnnotation("인디스페이스", coordinate: CLLocationCoordinate2DMake(37.570653,126.97164), exhibitions: nil, url: nil))
    }
    
    func createAnnotation(title: String, coordinate: CLLocationCoordinate2D, exhibitions: [String]?, url: String?) -> MKPointAnnotation {
        let point = MKPointAnnotation()
        point.title = title
        point.coordinate = coordinate
//        if let e = exhibitions {
//            point.exhibitions = exhibitions
//        }
        return point
    }
    
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("ANNOTATION")
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "ANNOTATION")
            annotationView!.canShowCallout = true
        }
        else{
            annotationView!.annotation = annotation
        }
        
        // 커스텀 어노테이션 종류가 여러 개 일 때
        if annotation is CustomAnnotation {
            // 커스텀 어노테이션 뷰 제공
        }
        
        return annotationView
        
    }
}
 
 
 
cs


반응형

댓글