์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- designpattern
- SwiftUI
- combine
- BOJ
- ํจ์คํธ์บ ํผ์ค
- realm
- ๋ฐฑ์ค
- CollectionView
- ํ๋ก๊ทธ๋๋จธ์ค
- node.js
- RxSwift
- Xcode
- reactorkit
- UIKit
- Swfit
- tableView
- raywenderlich
- Lv2
- visionOS
- arkit
- SnapKit
- BFS
- swift
- ios
- Kuring
- rxcocoa
- XCTest
- Flutter
- TCA
- MVVM
Archives
- Today
- Total
lgvv98
Map - ์ฝ๋๋ฆฌ๋ทฐ(๋งต๋ทฐ) ๋ณธ๋ฌธ
โ ๏ธ deprecated โ ๏ธ/Doit ์์ดํฐ ์ฑ(์
๋ฌธ)
Map - ์ฝ๋๋ฆฌ๋ทฐ(๋งต๋ทฐ)
๐ฅ ์บ๋ฟ๋งจ 2021. 3. 2. 12:55
//
// ViewController.swift
// Map
//
// Created by Hamlit Jason on 2021/02/21.
//
import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
// ๊ถํ์ด ์๋จ๋ฉด ์ด๊ธฐ์ ํ์ฉํด๋์ด์ ๊ทธ๋ ๊ณ , plist ์์ ์ ์ฑ
256์ชฝ ์ฐธ๊ณ
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
lblLocationInfo1.text = "" // ์์น ์ ๋ณด๋ฅผ ํ์ํ ๋ ์ด๋ธ
lblLocationInfo2.text = "" // ์์น ์ ๋ณด๋ฅผ ํ์ํ ๋ ์ด๋ธ
locationManager.delegate = self // ์์ ๋ก์ผ์ด์
๋ฉ๋์ ์ ๋ธ๋ฆฌ๊ฒ์ดํธ๋ฅผ ์
๋กํ ์ค์
locationManager.desiredAccuracy = kCLLocationAccuracyBest // ์ ํ๋๋ฅผ ์ต๊ณ ๋ก ์ค์
locationManager.requestWhenInUseAuthorization() // ์์น ๋ฐ์ดํฐ๋ฅผ ์ถ์ ํ๊ธฐ ์ํด ์ฌ์ฉ์์๊ฒ ์น์ธ์ ์๊ตฌํ๋ ์ฝ๋
locationManager.startUpdatingLocation() // ์์น ์
๋ฐ์ดํธ ์์
myMap.showsUserLocation = true // ์์น ๋ณด๊ธฐ ๊ฐ์ ํธ๋ฃจ๋ก ์ค์
}
let locationManager = CLLocationManager()
@IBOutlet var myMap: MKMapView!
@IBOutlet var lblLocationInfo1: UILabel!
@IBOutlet var lblLocationInfo2: UILabel!
@IBAction func sgChangeLocation(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
self.lblLocationInfo1.text = "" // ๋ ์ด๋ธ ์ด๊ธฐํ
self.lblLocationInfo2.text = "" // ๋ ์ด๋ธ ์ด๊ธฐํ
locationManager.startUpdatingLocation() // ์์น ์
๋ฐ์ดํธํ๋ ์ฝ๋
} else if sender.selectedSegmentIndex == 1 {
setAnnotation(latitudeValue: 37.73728281049552, longitudeValue: 128.89024026920865, delta: 1, title: "ํด๋ฆฌํ
๋ํ ๊ฐ๋ฆ์บ ํผ์ค", subtitle: "๊ฐ์๋ ๊ฐ๋ฆ์ ๋จ์ฐ์ด๊ต๊ธธ 121") // ๋ธํ ๊ฐ์ ํ๋๋น์จ๋ก ๊ธฐ์ค์ด 1์ด๊ณ ์ด๋ณด๋ค ์์ผ๋ฉด ํ๋ ํฌ๋ฉด ์ค์์์ด๋ค.
self.lblLocationInfo1.text = "๋ณด๊ณ ๊ณ์ ์์น"
self.lblLocationInfo2.text = "ํ๊ตญ ํด๋ฆฌํ
๋ํ ๊ฐ๋ฆ์บ ํผ์ค"
} else if sender.selectedSegmentIndex == 2 {
setAnnotation(latitudeValue: 37.556876, longitudeValue: 126.914066, delta: 0.1, title: "์ด์ง ํผ๋ธ๋ฆฌ์ฑ", subtitle: "์์ธ์ ๋งํฌ๊ตฌ ์๋ค๋ฆฌ๋ก 109 ์ด์ง์ค ๋น๋ฉ")
self.lblLocationInfo1.text = "๋ณด๊ณ ๊ณ์ ์์น"
self.lblLocationInfo2.text = "์ด์งํผ๋ธ๋ฆฌ์ฑ ์ถํ์ฌ"
} else if sender.selectedSegmentIndex == 3 {
setAnnotation(latitudeValue: 37.5433183958374, longitudeValue: 127.08835455546703, delta: 0.1, title: "์์ทจ๋ฐฉ", subtitle: "์์ธ์ ๊ด์ง๊ตฌ ์์๋ก 30๊ธธ 63")
self.lblLocationInfo1.text = "๋ณด๊ณ ๊ณ์ ์์น"
self.lblLocationInfo2.text = "์์ทจ๋ฐฉ"
}
}
func goLocation(latitudeValue : CLLocationDegrees, longitudeValue : CLLocationDegrees, delta span : Double) -> CLLocationCoordinate2D {
// ์๋์ ๊ฒฝ๋๋ก ์ํ๋ ์์น๋ฅผ ํ์ํ๊ธฐ ์ํ ํจ์
let pLocation = CLLocationCoordinate2DMake(latitudeValue, longitudeValue) // ์๋ ๊ฐ๊ณผ ๊ฒฝ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ก ํ์ฌ ํจ์ํธ์ถํ์ฌ ๊ทธ๊ฒ์ ๋ฐ๋๋ค.
let spanValue = MKCoordinateSpan(latitudeDelta: span, longitudeDelta: span) // ๋ฒ์ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ก ํ์ฌ ํจ์๋ฅผ ํธ์ถํ๊ณ ๋ฆฌํด๊ฐ์ ๋ฐ๋๋ค
let pRegion = MKCoordinateRegion(center: pLocation, span: spanValue) // ์์ ๋ ์ ๋ณด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ํ์ฌ ๋ฆฌํด๊ฐ์ ๋ฐ๋๋ค.
myMap.setRegion(pRegion, animated: true) // ํจ์ ํธ์ถ!
return pLocation
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// ์์น๊ฐ ์
๋ฐ์ดํธ ๋์์ ๋ ์ง๋์ ์์น๋ฅผ ๋ํ๋ด๊ธฐ ์ํ ํจ์
let pLocation = locations.last // ์์น๊ฐ ์
๋ฐ์ดํธ ๋๋ฉด ๋จผ์ ๋ง์ง๋ง ์์น ๊ฐ์ ์ฐพ์๋ธ๋ค
_ = goLocation(latitudeValue: (pLocation?.coordinate.latitude)!, longitudeValue: (pLocation?.coordinate.longitude)!, delta: 0.01) // ๋ง์ง๋ง ์์น์ ์๋์ ๊ฒฝ๋ ๊ฐ์ ๊ฐ์ง๊ณ ์์์ ๋ง๋ goLocation ํจ์๋ฅผ ํธ์ถํ๋ค. ์ด๋ delta ๊ฐ์ ์ง๋์ ํฌ๊ธฐ๋ฅผ ์ ํ๋๋ฐ, ๊ฐ์ด ์์์๋ก ํ๋๋๋ ํจ๊ณผ๊ฐ ์์ต๋๋ค. delta 0.01๋ก ํ์์ผ๋ 1์ ๊ฐ๋ณด๋ค ์ง๋๋ฅผ 100๋ฐฐ๋ก ํ๋ํด์ ๋ณด์ฌ์ค ๊ฒ์
๋๋ค. _ ํ์๋ ์๋์ด๋ ์๋ฌ๋ฅผ ๋ง๊ธฐ ์ํ ์ฝ๋
CLGeocoder().reverseGeocodeLocation(pLocation!, completionHandler: {
/*
ํ๋ ์ด์ค ๋งํฌ ๊ฐ์ ์ฒซ ๋ถ๋ถ๋ง pm์์๋ก ๋ฐ๋๋ค.
country ๋๋ผ
locality ์ง์ญ
thoroughfare ๋๋ก
๋๋ผ ์ง์ญ ๋๋ก --> ๊ณต๋ฐฑ๋ฃ์ด ์ฝ๊ธฐ ์ฝ๊ฒํ๊ธฐ
*/
(placemarks, error) -> Void in
let pm = placemarks!.first // ํ๋ ์ด์ค๋งํฌ ๊ฐ์ ์ฒซ ๋ถ๋ถ๋ง pm์์๋ก ๋์
ํ๋ค
let country = pm!.country // pm์์์์ ๋๋ผ ๊ฐ์ ์ปจํธ๋ก ์์์ ๋์
var address: String = country! // ๋ฌธ์์ด address์ country ์์์ ๊ฐ์ ๋์
ํฉ๋๋ค.
if pm!.locality != nil { // pm ์์์์ ๋๋ก ๊ฐ์ด ์กด์ฌํ๋ฉด adress ๋ฌธ์์ด์ ์ถ๊ฐํฉ๋๋ค
address += " " // ๊ณต๋ฐฑ๋ฃ์ด์ ์ฝ๊ธฐ ์ฝ๊ฒ ํ๋ ค๊ณ .
address += pm!.thoroughfare!
}
self.lblLocationInfo1.text = "ํ์ฌ์์น"
self.lblLocationInfo2.text = address
}) // ์๋์ ๊ฒฝ๋๊ฐ์ ๊ฐ๊ณ ์ญ์ผ๋ก ์ฃผ์๋ฅผ ์ฐพ์๋ณด๊ฒ ๋ค. ํธ๋ค๋ฌ์ ์ต๋ช
ํจ์๋ฅผ ์ถ๊ฐ๋ก ์ค๋นํด๊ณ ์ต๋ช
ํจ์๋ก ์ฒ๋ฆฌํ๋ค
locationManager.stopUpdatingLocation() // ๋ง์ง๋ง์ผ๋ก ์์น๊ฐ ์
๋ฐ์ดํธ๋๋ ๊ฒ์ ๋ฉ์ถ๊ฒ ํ๋ค.
}
func setAnnotation(latitudeValue : CLLocationDegrees, longitudeValue : CLLocationDegrees, delta span : Double, title strTitle : String, subtitle strSubtitle : String) {
let annotation = MKPointAnnotation() // ํ์ ์ค์ทจํ๊ธฐ ์ํด mkํฌ์ธํธ์ด๋
ธํ
์ด์
ํจ์ ํธ์ถํ์ฌ ๋ฆฌํด ๊ฐ์ ๋ฐ๋๋ค.
annotation.coordinate = goLocation(latitudeValue: latitudeValue, longitudeValue: longitudeValue, delta: span) // ์ด๋
ธํ
์ด์
์ ์ฟ ๋๋ค์ดํธ ๊ฐ์ ๊ณ ๋ก์ผ์ด์
ํจ์๋ก๋ถํฐ 2Dํํ๋ก ๋ฐ์ด์ผํ๋๋ฐ, ์ด๋ฅผ ์ํด์๋ goLocation ํจ์์ ๋ฆฌํด๊ฐ์ด ์๊ฒ ์์ ํด์ผ ํ๋ค.
//annotation.coordinate = goLocation(latitudeValue: latitudeValue, longitudeValue: longitudeValue, delta: span)
annotation.title = strTitle // ํ์ ํ์ดํ
annotation.subtitle = strSubtitle // ํ์ ์๋ธํ์ดํ
myMap.addAnnotation(annotation) // ๋งต ๋ทฐ์ ๋ณ์ ์ด๋
ธํ
์ด์
๊ฐ์ ์ถ๊ฐ
}
}
์ฌ๊ธฐ ์ฝ๋๋ ์ ํ์์ ์ง์ํด์ฃผ๋ ๊ฒ์ด๊ณ , ์นด์นด์ค๋ ๊ตฌ๊ธ ๋ฑ api๋ฅผ ์ด์ฉํ์ฌ ๊ณต๋ถํ ์ ์๋๋ก ํ์ฅํ์..!
์ฃผ์์ ๋ณด๋ฉด์ ์ดํดํ๋ฉด ์ฝ๋ค.
'โ ๏ธ deprecated โ ๏ธ > Doit ์์ดํฐ ์ฑ(์ ๋ฌธ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Tab - ์ฝ๋๋ฆฌ๋ทฐ (ํญ๋ฐ ์ปจํธ๋กค๋ฌ) (0) | 2021.03.02 |
---|---|
PageControl - ์ฝ๋๋ฆฌ๋ทฐ (0) | 2021.03.02 |
Web - ์ฝ๋๋ฆฌ๋ทฐ(์น๋ทฐ) (0) | 2021.03.02 |
Alert - ์ฝ๋๋ฆฌ๋ทฐ (0) | 2021.03.02 |
PickerView - ์ฝ๋๋ฆฌ๋ทฐ (0) | 2021.03.02 |
Comments