-
Notifications
You must be signed in to change notification settings - Fork 0
mapkit annotation
tyler edited this page Oct 10, 2023
·
1 revision
1. μ’νλ₯Ό CLLocationCoordinate2D νμ
μΌλ‘ μ μ₯
2. μ μ₯ν annotations λ₯Ό λ§΅ μμ νμ
3. Annotations 컀μ€ν
νκΈ°
- μ’νλ₯Ό μ μ₯νλ λ°©λ²μ λκ°μ§ μ λλ€.
- νμ§λ§ μ λͺ©, μ΄λ―Έμ§ μ΄λ¦λ±
Custom Annotationμ μν΄μλ λ°λ‘ ꡬ쑰체λ₯Ό λ§λ€μ΄ μ μ₯νλ κ²μ΄ μ’μ΅λλ€.
-
extension CLLocationCoordinate2Dμμ μμλ₯Ό λμ΄.duckRunμΌλ‘ μ½κ² μ¬μ©ν μ μκ² νλ λ°©λ²
extension CLLocationCoordinate2D {
static let duckRun = CLLocationCoordinate2D(
latitude: 37.54161,
longitude: 127.07648
)
}- λ³μλ₯Ό μ μ₯νλ λ°©λ²
// 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")
]- λ°μμ¨ annotatinos λ³μλ₯Ό κΈ°λ°μΌλ‘ κ°μ₯ κ°λ¨νκ² annotation μ λ§΅ μμ νμνλ λ°©λ²μ μλμ μ½λλ‘ κ°λ₯ν©λλ€.
for annotation in annotations {
let pointAnnotation = MKPointAnnotation()
pointAnnotation.coordinate = annotation.coordinate
pointAnnotation.title = annotation.title
mapView.addAnnotation(pointAnnotation)
}-
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
}
}