๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/iOS

[iOS] ์• ํ”Œ์ง€๋„์— ์›ํ•˜๋Š” ์œ„์น˜์— ๊ธ€์”จ์™€ ์ด๋ฏธ์ง€ ๋„ฃ๊ธฐ

by Fomagran ๐Ÿ’ป 2021. 3. 16.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” 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
    }

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์›ํ•˜๋Š” ์ด๋ฏธ์ง€์™€ ๊ทธ ์œ„์— ์›ํ•˜๋Š” ๋ ˆ์ด๋ธ”์ด ๋– ์žˆ๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

 

์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ์ด๋ฏธ์ง€์™€ ๋ ˆ์ด๋ธ”์„ ์›ํ•˜๋Š” ์œ„์น˜์— ๋„์›Œ๋ณด๋Š” ๊ฒƒ์„ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.

 

ํ˜น์‹œ๋ผ๋„ ๊ถ๊ธˆํ•œ ์ ์ด ์žˆ๊ฑฐ๋‚˜ ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”~!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€