본문 바로가기
Project/HiddenGem

🗺️ 지도 표시하기

by 밤새는 탐험가89 2025. 6. 2.
728x90
SMALL
import UIKit
import MapKit

class DetailMapInfoCell: UICollectionViewCell {
    
    static let reuseIdentifier: String = "DetailMapInfoCell"
    
    // MARK: - UI Components
    private let containerView: UIView = UIView()
    private let mapView: MKMapView = MKMapView()
    private let addressLabel: UILabel = UILabel()
    private let mapExpandButton: UIButton = UIButton()

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.backgroundColor = .systemBackground
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // containerView 설정
        containerView.layer.borderWidth = 0.5
        containerView.layer.borderColor = UIColor.black.cgColor
        containerView.layer.cornerRadius = 8
        containerView.clipsToBounds = true
        
        // ✅ 지도 인터랙션 허용
        mapView.isUserInteractionEnabled = true

        // 주소 라벨 설정
        addressLabel.font = .systemFont(ofSize: 14, weight: .semibold)
        addressLabel.textAlignment = .left
        addressLabel.textColor = .label
        addressLabel.numberOfLines = 1

        // 버튼 설정
        let iconImage = UIImage(named: "map_expand_icon") // 네가 첨부한 이미지 이름으로 추가 (Assets에 넣어야 함)
        mapExpandButton.setImage(iconImage, for: .normal)
        mapExpandButton.addTarget(self, action: #selector(didTapExpandButton), for: .touchUpInside)

        // 뷰 계층 구성
        contentView.addSubview(containerView)
        containerView.addSubview(mapView)
        containerView.addSubview(addressLabel)
        containerView.addSubview(mapExpandButton)
        
        // 오토레이아웃
        containerView.translatesAutoresizingMaskIntoConstraints = false
        mapView.translatesAutoresizingMaskIntoConstraints = false
        addressLabel.translatesAutoresizingMaskIntoConstraints = false
        mapExpandButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            containerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            containerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            containerView.topAnchor.constraint(equalTo: contentView.topAnchor),
            containerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
            
            mapView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            mapView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            mapView.topAnchor.constraint(equalTo: containerView.topAnchor),
            mapView.heightAnchor.constraint(equalToConstant: 250),
            
            addressLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 8),
            addressLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -8),
            addressLabel.topAnchor.constraint(equalTo: mapView.bottomAnchor, constant: 8),
            addressLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -8),
            
            // ✅ 버튼은 지도 우측 하단에 위치
            mapExpandButton.trailingAnchor.constraint(equalTo: mapView.trailingAnchor, constant: -8),
            mapExpandButton.bottomAnchor.constraint(equalTo: mapView.bottomAnchor, constant: -8),
            mapExpandButton.widthAnchor.constraint(equalToConstant: 30),
            mapExpandButton.heightAnchor.constraint(equalToConstant: 30)
        ])
    }

    func configure(with item: Mapinfo) {
        addressLabel.text = item.address
        
        guard let lat = Double(item.mapY),
              let lon = Double(item.mapX)
        else { return }

        let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        let region = MKCoordinateRegion(
            center: coordinate,
            span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
        )
        
        mapView.setRegion(region, animated: false)
        mapView.removeAnnotations(mapView.annotations) // 중복 방지
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        mapView.addAnnotation(annotation)
    }
    
    // MARK: - Action
    @objc private func didTapExpandButton() {
        print("지도페이지로 이동")
    }
}

 

728x90
LIST