diff --git a/Projects/App/Sources/Extension/LocationManager.swift b/Projects/App/Sources/Extension/LocationManager.swift new file mode 100644 index 0000000..54dfda5 --- /dev/null +++ b/Projects/App/Sources/Extension/LocationManager.swift @@ -0,0 +1,52 @@ +// +// LocationManager.swift +// Expo-iOS +// +// Created by 서지완 on 11/13/24. +// Copyright © 2024 SchoolofCompany. All rights reserved. +// + +import CoreLocation + +class LocationManager: ObservableObject { + func getCoordinateFromAddress(address: String, completion: @escaping (CLLocationCoordinate2D?) -> Void) { + let geocoder = CLGeocoder() + geocoder.geocodeAddressString(address) { (placemarks, error) in + guard let placemark = placemarks?.first, + let location = placemark.location else { + completion(nil) + return + } + completion(location.coordinate) + } + } +} + +/// *사용 예시 코드* +//import SwiftUI +//import CoreLocation +// +//struct ContentView: View { +// @StateObject private var locationManager = LocationManager() +// @State private var address = "" +// @State private var coordinate: CLLocationCoordinate2D? +// +// var body: some View { +// VStack { +// TextField("주소를 입력하세요", text: $address) +// .textFieldStyle(RoundedBorderTextFieldStyle()) +// .padding() +// +// Button("좌표 변환") { +// locationManager.getCoordinateFromAddress(address: address) { result in +// self.coordinate = result +// } +// } +// +// if let coordinate = coordinate { +// Text("위도: \(coordinate.latitude)") +// Text("경도: \(coordinate.longitude)") +// } +// } +// } +//}