[iOS] μ νμ§λμ μνλ μμΉμ κΈμ¨μ μ΄λ―Έμ§ λ£κΈ°
μλ νμΈμ Foma μ λλ€!
μ λ² μκ°μλ μ νμ§λμ μνλ μμΉ νμνλ λ²μ λν΄μ μμ보μλλ°μ.
(νΉμ μ보μ λΆλ€μ μ¬κΈ° λ₯Ό ν΅ν΄μ λ³΄κ³ μμ£ΌμΈμ!)
μ¬κΈ°μ λ μ κ·Έλ μ΄λ ν΄μ μνλ μμΉμ μ λ κ² μ ν΄μ§ λ§ν¬ νμλ§κ³ λ΄κ° μνλ μ΄λ―Έμ§λ λ μ΄λΈμ
λ£μ μ μμκΉ? νλ€κ° μ°Ύμλ΄μ μ 리νκ² λμμ΅λλ€.
λ°λ‘ μμν κ²μ~
MKMapViewDelegate
λ¨Όμ μ§λ μκ°μ λ§ν¬λ₯Ό νμνλ ViewControllerμ MKMapViewDelegateλ₯Ό μ±νν΄μ£ΌμΈμ!
class ViewController: UIViewController ,MKMapViewDelegate{
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
}
그리곀 μλμ κ°μ΄ annotationμ MKAnnotationViewλ‘ λ°νν΄μ£Όλ λ©μλλ₯Ό λ§λ€μ΄μ£ΌμΈμ!
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
κ·Έλ¦¬κ³ μ ν¨μμ annotationViewλ₯Ό λ§λ€μ΄μ£Όμκ³
μνλ μ΄λ―Έμ§λ₯Ό λ£μ μ΄λ―Έμ§λ·°μ λ μ΄λΈμ κ°κ° λ§λ€μ΄μ€λλ€.
μ¬κΈ°μ λ μ΄λΈμ yκ°μ 보면 -35λΌκ³ λμ΄μλλ°μ.
μνλ μμΉμμ μ‘°κΈ μμ λ μ΄λΈμ λμ°κΈ° λλ¬Έμ λλ€.
var annotationView: MKAnnotationView?
//μνλ μμΉμ λ£μ μ΄λ―Έμ§λ·°
let annotationimageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
let image = μνλ μ΄λ―Έμ§
annotationimageView.image = image
//μνλ μμΉμ λ£μ λ μ΄λΈ
let annotationLabel = UILabel(frame: CGRect(x: 0, y: -35, width: 45, height: 15))
annotationLabel.backgroundColor = .systemOrange
annotationLabel.textColor = .white
annotationLabel.numberOfLines = 3
annotationLabel.textAlignment = .center
annotationLabel.font = UIFont.boldSystemFont(ofSize: 10)
annotationLabel.text = annotation.title!
μμμ λ§λ μ΄λ―Έμ§λ·°μ λ μ΄λΈμ annotationViewμ μλΈλ·°λ‘ μΆκ°ν΄μ€λλ€.
κ·Έλ¦¬κ³ λ§μ§λ§μΌλ‘ annotationViewλ₯Ό λ°νν΄μ£ΌμΈμ.
annotationView?.addSubview(annotationimageView)
annotationView?.addSubview(annotationLabel)
return annotationView
}
μ΄λ κ² νλ©΄ μλμ κ°μ΄ μνλ μ΄λ―Έμ§μ κ·Έ μμ μνλ λ μ΄λΈμ΄ λ μλ λͺ¨μ΅μ λ³Ό μ μμ΅λλ€!
μ€λμ μ΄λ κ² μ΄λ―Έμ§μ λ μ΄λΈμ μνλ μμΉμ λμ보λ κ²μ μμ보μλλ°μ.
νΉμλΌλ κΆκΈν μ μ΄ μκ±°λ νλ¦° λΆλΆμ΄ μλ€λ©΄ λκΈλ‘ μλ €μ£ΌμΈμ~!