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

[iOS] ์• ํ”Œ์ง€๋„ ์ด์šฉํ•ด์„œ ์›ํ•˜๋Š” ์œ„์น˜ ํ‘œ์‹œํ•˜๊ธฐ(Mark Location Using MapKit)

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

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค.

 

์˜ค๋Š˜ ์•Œ์•„๋ณผ๊ฑด ์ฝ”์ฝ”์•„ ํ„ฐ์น˜ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ œ๊ณตํ•˜๋Š” MapKit์„ ์ด์šฉํ•ด์„œ ์›ํ•˜๋Š” ์œ„์น˜์— ๋งˆํฌ๋ฅผ ํ•ด๋ณผ๊ฑฐ์—์š”.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค~


Storyboard

 

๊ฐ€์žฅ ๋จผ์ € ์Šคํ† ๋ฆฌ๋ณด๋“œ๋กœ ์ด๋™ํ•ด์„œ 

 

Map Kit View๋ฅผ ๊ฒ€์ƒ‰ํ•œ ๋’ค 

์•„๋ž˜์™€ ๊ฐ™์ด ViewController์— ์„ธํŒ…ํ•ด์ฃผ์„ธ์š”!


ViewController

 

๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์™€์„œ ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ๋งŒ๋“ค์–ด์ค€ mapkitview๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ์„ธ์š”!

 @IBOutlet weak var mapView: MKMapView!

Marker

 

๊ทธ ๋‹ค์Œ ์›ํ•˜๋Š” ์œ„์น˜์— ํ‘œ์‹œ๋  Marker ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

title๊ณผ subtitle์€ ์›ํ•˜๋Š” ์œ„์น˜๋ฅผ ํ„ฐ์น˜ํ–ˆ์„๋•Œ ์ด๋ฆ„๊ณผ ์„ค๋ช…์„ ๋œปํ•˜๊ตฌ์š”.

 

coordinate๋Š” ์›ํ•˜๋Š” ์œ„์น˜๋ฅผ ์œ„๋„์™€ ๊ฒฝ๋„๋กœ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

import MapKit

class Marker: NSObject, MKAnnotation {
  let title: String?
  let coordinate: CLLocationCoordinate2D
  let subtitle:String?

  init(
    title: String?,
    subtitle: String?,
    coordinate: CLLocationCoordinate2D
  ) {
    self.title = title
    self.subtitle = subtitle
    self.coordinate = coordinate

    super.init()
  }

}

ViewController

 

๋‹ค์‹œ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋Œ์•„์™€์„œ

 

์œ„์—์„œ ๋งŒ๋“ค์–ด์ค€ Marker ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์›ํ•˜๋Š” ์ด๋ฆ„๊ณผ ์„ค๋ช… ์œ„์น˜๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

(์ €๋Š” ํ™๋Œ€์ž…๊ตฌ์—ญ์œผ๋กœ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.)

 let mark = Marker(
          title: "ํ™๋Œ€์ž…๊ตฌ์—ญ",
          subtitle: "์‚ฌ๋žŒ์ด ๋„ˆ๋ฌด ๋งŽ์•„์š”ใ…œ",
          coordinate: CLLocationCoordinate2D(latitude: 37.55769, longitude: 126.92450))
          
//์ด ๋ถ€๋ถ„์€ viewDidLoad() ์•ˆ์— ๋„ฃ์–ด ์‹คํ–‰์‹œ์ผœ์ฃผ์„ธ์š”!
mapView.addAnnotation(mark)

Google Map

 

์—ฌ๊ธฐ์„œ ์›ํ•˜๋Š” ์œ„์น˜์˜ ์œ„๋„์™€ ๊ฒฝ๋„๋ฅผ ์•Œ์•„์˜ค๋Š” ๋ฒ•์€ ๊ตฌ๊ธ€๋งต์„ ์ด์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

๊ตฌ๊ธ€์— ๊ตฌ๊ธ€๋งต์ด๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•œ ๋‹ค์Œ Google ์ง€๋„๋กœ ์ด๋™ํ•ด์ฃผ์„ธ์š”.

 

 

๊ทธ๋ฆฌ๊ณ  ์›ํ•˜๋Š” ์œ„์น˜๋ฅผ ๊ฒ€์ƒ‰ํ•œ ๋’ค

 

 

์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค ํด๋ฆญ์„ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์œ„๋„์™€ ๊ฒฝ๋„๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค!

 

 

์œ„์—์„œ ๋งŒ๋“ค์–ด์ค€ mark๋ฅผ viewDidLoad์—์„œ ์‹คํ–‰์‹œ์ผœ์ฃผ๋ฉด

 

์•„๋ž˜์™€ ๊ฐ™์ด ์›ํ•˜๋Š” ์œ„์น˜๊ฐ€ ํ‘œ์‹œ๋˜๊ณ 

 

 

ํ•ด๋‹น ์ง€์—ญ์„ ํ„ฐ์น˜ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ด๋ฆ„๊ณผ ์„ค๋ช…์ด ํ‘œ์‹œ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!

 

 

์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ์• ํ”Œ๋งต์„ ์ด์šฉํ•˜์—ฌ ์ง€๋„์— ์›ํ•˜๋Š” ์œ„์น˜๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.

 

๋‹ค์Œ ์‹œ๊ฐ„์—๋Š” ์›ํ•˜๋Š” ์œ„์น˜์— ์ด๋ฏธ์ง€์™€ ๋ ˆ์ด๋ธ”์„ ๋„ฃ๋Š”๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€