Skip to content

Commit

Permalink
Merge pull request #18 from DeveloperAcademy-POSTECH/feat/11-displayi…
Browse files Browse the repository at this point in the history
…ng-map

[#11] 맵 추가 및 사용자 현재 위치 받기
  • Loading branch information
Minkyeong-Choi authored Oct 17, 2024
2 parents 1d15932 + 8155c77 commit 1a130ce
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
14 changes: 12 additions & 2 deletions TMT/TMT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
5B6DA5B82CBE551400613ACB /* MapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B6DA5B72CBE551400613ACB /* MapView.swift */; };
5B6DA5BA2CBE6F8C00613ACB /* LocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B6DA5B92CBE6F8700613ACB /* LocationManager.swift */; };
D867396D2CA933CD00FFE8ED /* TMTApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D867396C2CA933CD00FFE8ED /* TMTApp.swift */; };
D867396F2CA933CD00FFE8ED /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D867396E2CA933CD00FFE8ED /* ContentView.swift */; };
D86739712CA933CE00FFE8ED /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D86739702CA933CE00FFE8ED /* Assets.xcassets */; };
Expand All @@ -18,6 +20,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
5B6DA5B72CBE551400613ACB /* MapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapView.swift; sourceTree = "<group>"; };
5B6DA5B92CBE6F8700613ACB /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = "<group>"; };
D86739692CA933CD00FFE8ED /* TMT.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TMT.app; sourceTree = BUILT_PRODUCTS_DIR; };
D867396C2CA933CD00FFE8ED /* TMTApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TMTApp.swift; sourceTree = "<group>"; };
D867396E2CA933CD00FFE8ED /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -59,8 +63,10 @@
D867396B2CA933CD00FFE8ED /* TMT */ = {
isa = PBXGroup;
children = (
5B6DA5B92CBE6F8700613ACB /* LocationManager.swift */,
D867396C2CA933CD00FFE8ED /* TMTApp.swift */,
D867396E2CA933CD00FFE8ED /* ContentView.swift */,
5B6DA5B72CBE551400613ACB /* MapView.swift */,
D8D377E72CBE95B80043D103 /* BusSearch */,
D8D377E42CBE548F0043D103 /* Resource */,
D86739702CA933CE00FFE8ED /* Assets.xcassets */,
Expand Down Expand Up @@ -168,6 +174,8 @@
files = (
D8D377E92CBE95C30043D103 /* BusSearchModel.swift in Sources */,
D867396F2CA933CD00FFE8ED /* ContentView.swift in Sources */,
5B6DA5B82CBE551400613ACB /* MapView.swift in Sources */,
5B6DA5BA2CBE6F8C00613ACB /* LocationManager.swift in Sources */,
D867396D2CA933CD00FFE8ED /* TMTApp.swift in Sources */,
D8D377EB2CBE95CA0043D103 /* BusSearchViewModel.swift in Sources */,
D8D377ED2CBE95D10043D103 /* BusSearchView.swift in Sources */,
Expand Down Expand Up @@ -305,9 +313,10 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"TMT/Preview Content\"";
DEVELOPMENT_TEAM = 7P5S729LQZ;
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "We want your location haha";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down Expand Up @@ -336,9 +345,10 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"TMT/Preview Content\"";
DEVELOPMENT_TEAM = 7P5S729LQZ;
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "We want your location haha";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down
57 changes: 57 additions & 0 deletions TMT/TMT/LocationManager.swift
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)")
}
}
47 changes: 47 additions & 0 deletions TMT/TMT/MapView.swift
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()
}

0 comments on commit 1a130ce

Please sign in to comment.