Skip to content

Commit

Permalink
[Feat] Location Manager 추가 - #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin0331 committed Jan 29, 2025
1 parent dd6731b commit 8654cf3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Whidy-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
6BF2D86D2D4A414C00252A43 /* LatestSearch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BF2D86C2D4A414C00252A43 /* LatestSearch.swift */; };
6BF2D8782D4A471000252A43 /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 6BF2D8772D4A471000252A43 /* RealmSwift */; };
6BF2D8792D4A492900252A43 /* RealmSwift in Embed Frameworks */ = {isa = PBXBuildFile; productRef = 6BF2D8772D4A471000252A43 /* RealmSwift */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
6BF2D87C2D4A568E00252A43 /* LocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BF2D87B2D4A568E00252A43 /* LocationManager.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -241,6 +242,7 @@
6BF2D8632D49ED3D00252A43 /* SearchFeature.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchFeature.swift; sourceTree = "<group>"; };
6BF2D8652D49EEA800252A43 /* StudyMapFeature+Enum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "StudyMapFeature+Enum.swift"; sourceTree = "<group>"; };
6BF2D86C2D4A414C00252A43 /* LatestSearch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LatestSearch.swift; sourceTree = "<group>"; };
6BF2D87B2D4A568E00252A43 /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -556,6 +558,7 @@
6B62762F2D35EFFB00843C9E /* NetworkMonitor.swift */,
6B6276302D35EFFB00843C9E /* AppStoreCheckManager.swift */,
6BF2D8532D46037500252A43 /* NaverMapManager.swift */,
6BF2D87B2D4A568E00252A43 /* LocationManager.swift */,
6B6275E12D35EFE200843C9E /* Enviroment.swift */,
6B6275E22D35EFE200843C9E /* Logger.swift */,
6B6275E32D35EFE200843C9E /* LoginType.swift */,
Expand Down Expand Up @@ -947,6 +950,7 @@
6B6275F22D35EFE200843C9E /* Notification.name+Extension.swift in Sources */,
6B6275F32D35EFE200843C9E /* Array+Extension.swift in Sources */,
6B6276992D36199700843C9E /* RootCoordinatorView.swift in Sources */,
6BF2D87C2D4A568E00252A43 /* LocationManager.swift in Sources */,
6B6220AA2D3896E800C5F2C8 /* WhidySplashView.swift in Sources */,
6B62207C2D37AA9700C5F2C8 /* StudyMapCoordinator.swift in Sources */,
6B6275F42D35EFE200843C9E /* String+Extension.swift in Sources */,
Expand Down
41 changes: 41 additions & 0 deletions Whidy-iOS/Core/Utils/LocationManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// LocationManager.swift
// Whidy-iOS
//
// Created by JinwooLee on 1/29/25.
//

import Foundation
import CoreLocation

final class LocationManager: NSObject, CLLocationManagerDelegate {
static let shared = LocationManager()
private let locationManager = CLLocationManager()
private(set) var altitude: Double?
private(set) var latitude: Double?
private(set) var longitude: Double?

override private init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}

func getManager() -> CLLocationManager {
return locationManager
}

// CLLocationManagerDelegate 메서드
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
latitude = location.coordinate.latitude
longitude = location.coordinate.longitude
altitude = location.altitude
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
Logger.error("Failed to find user's location: \(error.localizedDescription)")
}
}
12 changes: 3 additions & 9 deletions Whidy-iOS/Core/Utils/NaverMapManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import NMapsMap

final class NaverMapManager : NSObject, ObservableObject, NMFMapViewCameraDelegate, NMFMapViewTouchDelegate, CLLocationManagerDelegate {
static let shared = NaverMapManager()
var locationManager: CLLocationManager?
private let locationManager = LocationManager.shared.getManager()

let view = NMFNaverMapView(frame: .zero)
@Published var coord: (Double, Double) = (0.0, 0.0)
Expand Down Expand Up @@ -38,8 +38,6 @@ final class NaverMapManager : NSObject, ObservableObject, NMFMapViewCameraDelega

// MARK: - 위치 정보 동의 확인
func checkLocationAuthorization() {
guard let locationManager = locationManager else { Logger.error("LocationManager Nil");return }

switch locationManager.authorizationStatus {
case .notDetermined:
Logger.debug("notDetermined")
Expand All @@ -65,8 +63,6 @@ final class NaverMapManager : NSObject, ObservableObject, NMFMapViewCameraDelega
DispatchQueue.global().async {
if CLLocationManager.locationServicesEnabled() {
DispatchQueue.main.async {
self.locationManager = CLLocationManager()
self.locationManager!.delegate = self
self.checkLocationAuthorization()
}
} else {
Expand All @@ -76,11 +72,9 @@ final class NaverMapManager : NSObject, ObservableObject, NMFMapViewCameraDelega
}

private func fetchUserLocation() {
if let locationManager = locationManager {
if let lat = locationManager.location?.coordinate.latitude, let lng = locationManager.location?.coordinate.longitude {
Logger.debug("fetchUserLocation Success ✅")
let lat = locationManager.location?.coordinate.latitude
let lng = locationManager.location?.coordinate.longitude
let cameraUpdate = NMFCameraUpdate(scrollTo: NMGLatLng(lat: lat ?? 0.0, lng: lng ?? 0.0), zoomTo: 15)
let cameraUpdate = NMFCameraUpdate(scrollTo: NMGLatLng(lat: lat, lng: lng), zoomTo: 15)
cameraUpdate.animation = .easeIn
cameraUpdate.animationDuration = 0.5

Expand Down

0 comments on commit 8654cf3

Please sign in to comment.