Skip to content

Commit

Permalink
Merge pull request #59 from School-of-Company/58-AddressChang-Coordin…
Browse files Browse the repository at this point in the history
…ates

🔀 :: [#58] 주소를 좌표로 변경하는 기능
  • Loading branch information
Xixn2 authored Nov 13, 2024
2 parents 553daa8 + 21085a0 commit 3398170
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Projects/App/Sources/Extension/LocationManager.swift
Original file line number Diff line number Diff line change
@@ -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)")
// }
// }
// }
//}

0 comments on commit 3398170

Please sign in to comment.