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

[iOS] ์• ํ”Œ์ง€๋„์— ๋ฒ”์œ„ ํ‘œ์‹œํ•˜๊ณ  ์•ˆ์— ํŠน์ • ์œ„์น˜๊ฐ€ ์†ํ•ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ(Make range and Verify location is inside with MapKit)

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

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

์ €๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์›ํ•˜๋Š” ์œ„์น˜๋ฅผ ํ‘œ์‹œํ•˜๊ณ  ์ด๋ฏธ์ง€์™€ ๋ ˆ์ด๋ธ”์„ ๋„ฃ์–ด๋ณด์•˜๋Š”๋ฐ์š”.

 

(ํ˜น์‹œ ์•ˆ๋ณด์‹  ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ๋ฅผ ํ†ตํ•ด์„œ ๋ณด๊ณ  ์™€์ฃผ์„ธ์š”!)

 

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์›ํ•˜๋Š” ์œ„์น˜๊ฐ€ ํŠน์ • ๋ฒ”์œ„ ์•ˆ์— ์†ํ•ด์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


MKMapViewDelegate ์ถ”๊ฐ€ํ•˜๊ธฐ

 

๊ฐ€์žฅ ๋จผ์ € ViewController์— MKMapViewDelegate๋ฅผ ์ฑ„ํƒํ•ด์ฃผ์„ธ์š”. (๋‹น์—ฐํžˆ import MapKit๋„ ํ•ด์•ผ๊ฒ ์ฃ ?)

 

class ViewController: UIViewController ,MKMapViewDelegate{...}

๋งˆ์ปค ์„ค์ •ํ•˜๊ธฐ

 

์›ํ•˜๋Š” ์œ„์น˜์— ํ‘œ์‹œํ•  ๋งˆ์ปค๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

 

์ €๋ฒˆ ์‹œ๊ฐ„์—๋Š” ํ™๋Œ€์ž…๊ตฌ์—ญ๋งŒ ํ–ˆ์ง€๋งŒ ํ™๋Œ€์ž…๊ตฌ์—ญ ๊ทผ์ฒ˜์ธ ์Šคํƒ€๋ฒ…์Šค๊นŒ์ง€ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

        let mark1 = Marker(
          title: "ํ™๋Œ€์ž…๊ตฌ์—ญ",
          subtitle: "์‚ฌ๋žŒ์ด ๋„ˆ๋ฌด ๋งŽ์•„์š”..ใ…œ",
          coordinate: CLLocationCoordinate2D(latitude: 37.55769, longitude: 126.92450))
        mapView.addAnnotation(mark1)
        
        let mark2 = Marker(
          title: "ํ™๋Œ€์ž…๊ตฌ์—ญ์Šคํƒ€๋ฒ…์Šค",
          subtitle: "์ปคํ”ผ ๋ง›์žˆ๋‹ค",
          coordinate: CLLocationCoordinate2D(latitude: 37.557123, longitude: 126.923588))
        mapView.addAnnotation(mark2)

๋ฒ”์œ„ ๊ทธ๋ ค์ฃผ๊ธฐ

 

๋งˆ์ปค์˜ ์œ„์น˜๋ฅผ ํ™•์ธํ•  ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ  ๊ทธ ์•„๋ž˜์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

 

๋จผ์ € CLLocationCoordinate2DMake ๋กœ ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค ์œ„์น˜์˜ ์ค‘์•™์˜ ์ขŒํ‘œ๊ฐ’์„ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณค ๋ฒ”์œ„๊ฐ’์„ ์ •ํ•ด์ฃผ๋Š”๋ฐ ์ด๊ฒƒ์€ meter๊ธฐ์ค€์ด๋ผ์„œ 100.0์„ ์ž…๋ ฅํ•ด์ฃผ์‹œ๋ฉด ๊ทผ๋ฐฉ 100m ๋ฒ”์œ„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋ฒ”์œ„๋„ ์„ธํŒ…ํ•ด์ฃผ์—ˆ๋‹ค๋ฉด ํ™๋Œ€์ž…๊ตฌ์—ญ ์ค‘์•™์œผ๋กœ๋ถ€ํ„ฐ ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค์–ด์•ผ๊ฒ ์ฃ ?

 

์•„๋ž˜์™€ ๊ฐ™์ด CLCircularRegion ์œผ๋กœ ์ค‘์•™์˜ ๊ฒฝ๋„์™€ ์œ„๋„๊ฐ’ ๋ฒ”์œ„ ์•„์ด๋ดํ‹ฐํ”ผ์–ด๋ฅผ ์จ์ค๋‹ˆ๋‹ค.

 

๊ทธ ๋‹ค์Œ์œผ๋กœ ์›๋ชจ์–‘์„ ๋งŒ๋“ค์–ด์„œ mapView์— Overlay๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!

  func detectMarkerLocation() {
        
           let ํ™๋Œ€์ž…๊ตฌ์—ญ์ค‘์•™ = CLLocationCoordinate2DMake(37.55769, 126.92450)
            let ๋ฒ”์œ„ = 100.0 //100meter๋ฅผ ๋œปํ•จ
            
            let ํ™๋Œ€์ž…๊ตฌ์—ญ๋ฒ”์œ„ = CLCircularRegion(center: CLLocationCoordinate2D(latitude: ํ™๋Œ€์ž…๊ตฌ์—ญ์ค‘์•™.latitude,
                                                                         longitude: ํ™๋Œ€์ž…๊ตฌ์—ญ์ค‘์•™.longitude), radius: ๋ฒ”์œ„, identifier: "ํ™๋Œ€์ž…๊ตฌ์—ญ")
            let ์›๋ชจ์–‘ = MKCircle(center: ํ™๋Œ€์ž…๊ตฌ์—ญ์ค‘์•™, radius: ๋ฒ”์œ„)
            mapView.addOverlay(์›๋ชจ์–‘)
            
			...
            }

์ด์ œ ์ถ”๊ฐ€ํ•ด์ค€ ์›๋ชจ์–‘ ์˜ค๋ฒ„๋ ˆ์ด๋ฅผ ๊ทธ๋ ค์ค˜์•ผ๊ฒ ์ฃ ?

 

์•„๋ž˜์™€ ๊ฐ™์ด MKCircleRenderer ์„ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ  

 

์›์˜ ๊ฐ€์žฅ์ž๋ฆฌ ์ƒ‰๊น”,์•ˆ์„ ์ฑ„์šธ์ƒ‰๊น”,๊ฐ€์žฅ์ž๋ฆฌ์˜ ๋‘๊ป˜๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

   func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let circleRenderer = MKCircleRenderer(overlay: overlay)
        circleRenderer.strokeColor = .red
        circleRenderer.fillColor = UIColor.yellow.withAlphaComponent(0.3)
        circleRenderer.lineWidth = 1.0
        return circleRenderer
    }

 

์ด๋ ‡๊ฒŒ ํ•˜๊ณ  ์‹คํ–‰์‹œ์ผœ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ™๋Œ€์ž…๊ตฌ์—ญ ๊ทผ๋ฐฉ 100m์˜ ๋ฒ”์œ„๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


์•ˆ์— ์†ํ•ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

 

์ด์ œ ๋ฒ”์œ„๋„ ๊ทธ๋ ค์คฌ์œผ๋‹ˆ ํ™๋Œ€์ž…๊ตฌ์—ญ ์Šคํƒ€๋ฒ…์Šค๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ๊ฒ ์ฃ ?

 

detectMarkerLoaction ํ•จ์ˆ˜์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

 

mapView์•ˆ์— ์ถ”๊ฐ€ํ•ด์ค€ marker๋“ค์„ for๋ฌธ์œผ๋กœ ํƒ์ƒ‰ํ•ด ํ•ด๋‹น ์œ„์น˜๊ฐ€ ํ™๋Œ€์ž…๊ตฌ์—ญ๋ฒ”์œ„์— ํฌํ•จ๋˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

            for marker in mapView.annotations{
                if ํ™๋Œ€์ž…๊ตฌ์—ญ๋ฒ”์œ„.contains(marker.coordinate) {
                    print("\(String(describing: marker.title))์€ ํ™๋Œ€์ž…๊ตฌ์—ญ ๋ฒ”์œ„์— ํฌํ•จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
                }else{
                    print("\(String(describing: marker.title))์€ ํ™๋Œ€์ž…๊ตฌ์—ญ ๋ฒ”์œ„์— ํฌํ•จ๋˜์ง€์•Š์Šต๋‹ˆ๋‹ค.")
                }

 

์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณด๋‹ˆ ์•„์Šฌ์•„์Šฌํ•˜๊ฒŒ ์Šคํƒ€๋ฒ…์Šค๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์•˜๋‚˜๋ณด๋„ค์š”.

 

 

๋ฒ”์œ„๋ฅผ ์กฐ๊ธˆ ๋Š˜๋ ค์ค๋‹ˆ๋‹ค.

 

 let ๋ฒ”์œ„ = 200.0

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ™•์‹คํžˆ ๋ฒ”์œ„๋„ ์ปค์กŒ๊ณ  ์Šคํƒ€๋ฒ…์Šค๊ฐ€ ์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ์ฃ ?

์ถœ๋ ฅํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์Šคํƒ€๋ฒ…์Šค๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๊ณ  ๋‚˜์˜ค๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ์›ํ•˜๋Š” ์œ„์น˜์— ๋ฒ”์œ„๋ฅผ ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์•ˆ์— ํŠน์ • ์œ„์น˜๊ฐ€ ์†ํ•ด์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

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

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€