-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from DeveloperAcademy-POSTECH/feat/11-displayi…
…ng-map [#11] 맵 추가 및 사용자 현재 위치 받기
- Loading branch information
Showing
3 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// LocationManagerDelegate.swift | ||
// TMT | ||
// | ||
// Created by Choi Minkyeong on 10/15/24. | ||
// | ||
|
||
import Foundation | ||
import CoreLocation | ||
import MapKit | ||
|
||
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { | ||
@Published var region = MKCoordinateRegion( | ||
center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194), | ||
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01) | ||
) | ||
|
||
private let locationManager = CLLocationManager() | ||
|
||
override init() { | ||
super.init() | ||
locationManager.delegate = self | ||
locationManager.desiredAccuracy = kCLLocationAccuracyBest | ||
locationManager.requestWhenInUseAuthorization() | ||
locationManager.startUpdatingLocation() | ||
} | ||
|
||
/// 사용자의 현재 위치를 파악합니다. | ||
func findCurrentLocation() { | ||
if let location = locationManager.location { | ||
let latitude = location.coordinate.latitude | ||
let longitude = location.coordinate.longitude | ||
|
||
region = MKCoordinateRegion( | ||
center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), | ||
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01) | ||
) | ||
} | ||
} | ||
|
||
/// 사용자의 위치를 업데이트합니다. | ||
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | ||
if let location = locations.last { | ||
DispatchQueue.main.async { | ||
self.region = MKCoordinateRegion( | ||
center: location.coordinate, | ||
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01) | ||
) | ||
} | ||
} | ||
} | ||
|
||
/// 사용자의 위치를 받아오지 못한 경우 | ||
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { | ||
print("Failed to find user's location: \(error.localizedDescription)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// MapView.swift | ||
// TMT | ||
// | ||
// Created by Choi Minkyeong on 10/15/24. | ||
// | ||
|
||
import SwiftUI | ||
import MapKit | ||
|
||
struct MapView: View { | ||
@StateObject private var locationManager = LocationManager() | ||
|
||
var body: some View { | ||
ZStack { | ||
Map(coordinateRegion: $locationManager.region, showsUserLocation: true) | ||
.edgesIgnoringSafeArea(.all) | ||
VStack { | ||
HStack { | ||
Spacer() | ||
Button { | ||
locationManager.findCurrentLocation() | ||
} label: { | ||
ZStack { | ||
Circle() | ||
.frame(width: 40) | ||
.tint(.white) | ||
.shadow(radius: 5) | ||
Image(systemName: "location.fill") | ||
.font(.title) | ||
.tint(.gray) | ||
} | ||
} | ||
} | ||
Spacer() | ||
} | ||
.padding() | ||
} | ||
.onAppear { | ||
locationManager.findCurrentLocation() | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
MapView() | ||
} |