Skip to content

mapkit annotation

tyler edited this page Oct 10, 2023 · 1 revision

πŸ“ μ§€λ„μœ„μ— 마크 ν‘œμ‹œ (Annotation)

λͺ©μ°¨

1. μ’Œν‘œλ₯Ό CLLocationCoordinate2D νƒ€μž…μœΌλ‘œ μ €μž₯
2. μ €μž₯ν•œ annotations λ₯Ό λ§΅ μœ„μ— ν‘œμ‹œ
3. Annotations μ»€μŠ€ν…€ ν•˜κΈ°

μ’Œν‘œλ₯Ό CLLocationCoordinate2D νƒ€μž…μœΌλ‘œ μ €μž₯

  • μ’Œν‘œλ₯Ό μ €μž₯ν•˜λŠ” 방법은 두가지 μž…λ‹ˆλ‹€.
  • ν•˜μ§€λ§Œ 제λͺ©, 이미지 이름등 Custom Annotation 을 μœ„ν•΄μ„œλŠ” λ”°λ‘œ ꡬ쑰체λ₯Ό λ§Œλ“€μ–΄ μ €μž₯ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.
  1. extension CLLocationCoordinate2D μ—μ„œ μƒμˆ˜λ₯Ό 두어 .duckRun 으둜 μ‰½κ²Œ μ‚¬μš©ν•  수 있게 ν•˜λŠ” 방법
extension CLLocationCoordinate2D {
    static let duckRun = CLLocationCoordinate2D(
        latitude: 37.54161,
        longitude: 127.07648
    )
}
  1. λ³€μˆ˜λ₯Ό μ €μž₯ν•˜λŠ” 방법
// AnnotationItem ꡬ쑰체 생성
struct AnnotationItem: Identifiable {
    let id = UUID()
    let coordinate: CLLocationCoordinate2D
    let title: String
    let imageName: String
}

    @State private var annotations: [AnnotationItem] = [
        // μœ„μ—μ„œ μ €μž₯ν•œ .duckRun 을 μ‚¬μš©ν•˜λŠ” λͺ¨μŠ΅
        AnnotationItem(coordinate: .duckRun, title: "였리런", imageName: "duck"),
        AnnotationItem(coordinate: CLLocationCoordinate2D(latitude: 37.53958, longitude: 127.07435), title: "μŠ€νƒ€νŠΈ 포인트", imageName: "startpoint")
    ]

μ €μž₯ν•œ annotations λ₯Ό λ§΅ μœ„μ— ν‘œμ‹œ

  • λ°›μ•„μ˜¨ annotatinos λ³€μˆ˜λ₯Ό 기반으둜 κ°€μž₯ κ°„λ‹¨ν•˜κ²Œ annotation 을 λ§΅ μœ„μ— ν‘œμ‹œν•˜λŠ” 방법은 μ•„λž˜μ˜ μ½”λ“œλ‘œ κ°€λŠ₯ν•©λ‹ˆλ‹€.
        for annotation in annotations {
            let pointAnnotation = MKPointAnnotation()
            pointAnnotation.coordinate = annotation.coordinate
            pointAnnotation.title = annotation.title
            mapView.addAnnotation(pointAnnotation)
        }

Annotations μ»€μŠ€ν…€ ν•˜κΈ°

  • Coordinator λ₯Ό ν™œμš©ν•˜μ—¬ Annotations 을 μ»€μŠ€ν…€ ν•©λ‹ˆλ‹€.
  • μ €λŠ” μ˜€λ¦¬λŸ°μ„ ν‘œμ‹œν•˜λŠ” λΆ€λΆ„κ³Ό μŠ€νƒ€νŠΈ 포인트λ₯Ό ν‘œμ‹œν•˜λŠ” 두 λΆ€λΆ„μœΌλ‘œ λ‚˜λˆ μ„œ Annotations λ₯Ό ν‘œμ‹œν–ˆμŠ΅λ‹ˆλ‹€.
func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, MKMapViewDelegate {
        var parent: MapView
        
        init(_ parent: MapView) {
            self.parent = parent
        }
        
        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            guard !(annotation is MKUserLocation) else {
                return nil
            }
            
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "custom")
            
            if annotationView == nil {
                annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "custom")
            } else {
                annotationView?.annotation = annotation
            }
            
            // annotation 의 제λͺ©μ„ 쑰건으둜 custom
            switch annotation.title {
            case "였리런":
                annotationView?.image = UIImage(named: "duck")
                let imageSize = CGSize(width: 30, height: 30)
                annotationView?.frame.size = imageSize
                
                let titleLabel = UILabel()
                titleLabel.text = annotation.title ?? ""
                titleLabel.textAlignment = .center
                titleLabel.font = UIFont.systemFont(ofSize: 12)
                titleLabel.frame = CGRect(x: 0, y: imageSize.height, width: imageSize.width * 3, height: 20)
                titleLabel.center = CGPoint(x: annotationView!.bounds.midX, y: annotationView!.bounds.midY + 25)
                annotationView?.addSubview(titleLabel)


            default:
                annotationView?.image = UIImage(named: "startpoint")
                let imageSize = CGSize(width: 20, height: 20)
                annotationView?.frame.size = imageSize
                
                let titleLabel = UILabel()
                titleLabel.text = annotation.title ?? ""
                titleLabel.textAlignment = .center
                titleLabel.font = UIFont.systemFont(ofSize: 10)
                titleLabel.frame = CGRect(x: 0, y: imageSize.height, width: imageSize.width * 3, height: 20)
                titleLabel.center = CGPoint(x: annotationView!.bounds.midX, y: annotationView!.bounds.midY + 20)
                annotationView?.addSubview(titleLabel)
                
            }
            
            annotationView?.clipsToBounds = false
            
            return annotationView
        }
    }

Clone this wiki locally