From dbc746757d9268fcd3052cdcf9da174acd5802a2 Mon Sep 17 00:00:00 2001 From: Minkyeong-Choi Date: Fri, 18 Oct 2024 03:24:01 +0900 Subject: [PATCH 1/4] feat: #12 Showing bus stop location and fixing some error --- TMT/TMT.xcodeproj/project.pbxproj | 6 +- .../xcshareddata/xcschemes/TMT.xcscheme | 82 +++++++++++++++++++ TMT/TMT/BusSearch/BusSearchViewModel.swift | 22 +++++ TMT/TMT/LocationManager.swift | 4 +- TMT/TMT/MapView.swift | 59 +++++++++---- 5 files changed, 151 insertions(+), 22 deletions(-) create mode 100644 TMT/TMT.xcodeproj/xcshareddata/xcschemes/TMT.xcscheme diff --git a/TMT/TMT.xcodeproj/project.pbxproj b/TMT/TMT.xcodeproj/project.pbxproj index 42308b2..e3a78db 100644 --- a/TMT/TMT.xcodeproj/project.pbxproj +++ b/TMT/TMT.xcodeproj/project.pbxproj @@ -7,9 +7,9 @@ objects = { /* Begin PBXBuildFile section */ - D844BCB52CC0AE770059E31F /* BusStopData.csv in Resources */ = {isa = PBXBuildFile; fileRef = D844BCB42CC0AE770059E31F /* BusStopData.csv */; }; 5B6DA5B82CBE551400613ACB /* MapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B6DA5B72CBE551400613ACB /* MapView.swift */; }; 5B6DA5BA2CBE6F8C00613ACB /* LocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B6DA5B92CBE6F8700613ACB /* LocationManager.swift */; }; + D844BCB52CC0AE770059E31F /* BusStopData.csv in Resources */ = {isa = PBXBuildFile; fileRef = D844BCB42CC0AE770059E31F /* BusStopData.csv */; }; 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 */; }; @@ -20,9 +20,9 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - D844BCB42CC0AE770059E31F /* BusStopData.csv */ = {isa = PBXFileReference; lastKnownFileType = text; path = BusStopData.csv; sourceTree = ""; }; 5B6DA5B72CBE551400613ACB /* MapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapView.swift; sourceTree = ""; }; 5B6DA5B92CBE6F8700613ACB /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = ""; }; + D844BCB42CC0AE770059E31F /* BusStopData.csv */ = {isa = PBXFileReference; lastKnownFileType = text; path = BusStopData.csv; sourceTree = ""; }; 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 = ""; }; D867396E2CA933CD00FFE8ED /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -63,10 +63,10 @@ D867396B2CA933CD00FFE8ED /* TMT */ = { isa = PBXGroup; children = ( - 5B6DA5B92CBE6F8700613ACB /* LocationManager.swift */, D867396C2CA933CD00FFE8ED /* TMTApp.swift */, D867396E2CA933CD00FFE8ED /* ContentView.swift */, 5B6DA5B72CBE551400613ACB /* MapView.swift */, + 5B6DA5B92CBE6F8700613ACB /* LocationManager.swift */, D8D377E72CBE95B80043D103 /* BusSearch */, D8D377E42CBE548F0043D103 /* Resource */, D86739702CA933CE00FFE8ED /* Assets.xcassets */, diff --git a/TMT/TMT.xcodeproj/xcshareddata/xcschemes/TMT.xcscheme b/TMT/TMT.xcodeproj/xcshareddata/xcschemes/TMT.xcscheme new file mode 100644 index 0000000..9abddef --- /dev/null +++ b/TMT/TMT.xcodeproj/xcshareddata/xcschemes/TMT.xcscheme @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TMT/TMT/BusSearch/BusSearchViewModel.swift b/TMT/TMT/BusSearch/BusSearchViewModel.swift index 7f0246a..54a5487 100644 --- a/TMT/TMT/BusSearch/BusSearchViewModel.swift +++ b/TMT/TMT/BusSearch/BusSearchViewModel.swift @@ -11,6 +11,7 @@ final class BusStopSearchViewModel: ObservableObject { @Published var busStops: [BusStopInfo] = [] @Published var filteredBusStops: [BusStopInfo] = [] @Published var busNumbers = [Int]() + @Published var nameAndCoordinates: [BusStop] = [] init() { loadCSV() @@ -72,4 +73,25 @@ final class BusStopSearchViewModel: ObservableObject { return nil })) } + + func getNameAndCoordinates(busNum: String) { + var stopInfo: [BusStop] = [] + for busStop in busStops { + if busStop.busNumber == busNum { + if let stopName = busStop.stopName, + let xCoordinate = busStop.xCoordinate, + let yCoordinate = busStop.yCoordinate { + stopInfo.append(BusStop(name: stopName, latitude: xCoordinate, longitude: yCoordinate)) + } + } + } + nameAndCoordinates = stopInfo + } +} + +struct BusStop: Identifiable { + let id = UUID() + let name: String + let latitude: String + let longitude: String } diff --git a/TMT/TMT/LocationManager.swift b/TMT/TMT/LocationManager.swift index 3095781..e78faf9 100644 --- a/TMT/TMT/LocationManager.swift +++ b/TMT/TMT/LocationManager.swift @@ -1,5 +1,5 @@ // -// LocationManagerDelegate.swift +// LocationManager.swift // TMT // // Created by Choi Minkyeong on 10/15/24. @@ -11,7 +11,7 @@ import MapKit class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { @Published var region = MKCoordinateRegion( - center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194), + center: CLLocationCoordinate2D(latitude: 36.016082, longitude: 129.324605), span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01) ) diff --git a/TMT/TMT/MapView.swift b/TMT/TMT/MapView.swift index 574fbd6..88b6f57 100644 --- a/TMT/TMT/MapView.swift +++ b/TMT/TMT/MapView.swift @@ -10,29 +10,54 @@ import MapKit struct MapView: View { @StateObject private var locationManager = LocationManager() + @StateObject var busStopSearchViewModel = BusStopSearchViewModel() + // TODO: 사용자 위치 바뀌어도 화면 다시 업데이트 안되게 하기. 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) - } + Map(coordinateRegion: $locationManager.region, showsUserLocation: true, annotationItems: busStopSearchViewModel.nameAndCoordinates) { stop in + MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: Double(stop.latitude) ?? 0, longitude: Double(stop.longitude) ?? 0)) { + VStack { + RoundedRectangle(cornerRadius: 5) + .frame(width: 30, height: 30) + .foregroundStyle(.blue) + Text(stop.name) + .font(.caption) + .foregroundColor(.black) } } + } + .edgesIgnoringSafeArea(.all) + VStack { + Button { + busStopSearchViewModel.getNameAndCoordinates(busNum: "207(기본)") + } label: { + Text("207번 버스") + .background(.white) + } + + Button { + busStopSearchViewModel.getNameAndCoordinates(busNum: "306(기본)") + } label: { + Text("472번 버스") + .background(.white) + } + Spacer() + Button { + locationManager.findCurrentLocation() + } label: { + ZStack { + Circle() + .frame(width: 40) + .tint(.white) + .shadow(radius: 5) + Image(systemName: "location.fill") + .font(.title) + .tint(.gray) + } + } + } .padding() } From 66d3aa7a69ee3d1194ab92b4ef1c761c57570b38 Mon Sep 17 00:00:00 2001 From: Minkyeong-Choi Date: Fri, 18 Oct 2024 19:53:32 +0900 Subject: [PATCH 2/4] fix: #12 fix BusStopInfo struct --- TMT/TMT/BusSearch/BusSearchModel.swift | 5 ++++- TMT/TMT/BusSearch/BusSearchViewModel.swift | 15 ++++----------- TMT/TMT/MapView.swift | 6 ++---- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/TMT/TMT/BusSearch/BusSearchModel.swift b/TMT/TMT/BusSearch/BusSearchModel.swift index e2b6b2d..d828649 100644 --- a/TMT/TMT/BusSearch/BusSearchModel.swift +++ b/TMT/TMT/BusSearch/BusSearchModel.swift @@ -5,7 +5,10 @@ // Created by 김유빈 on 10/15/24. // -struct BusStopInfo: Codable { +import Foundation + +struct BusStopInfo: Codable, Identifiable { + var id = UUID() var busNumber: String? // 노선명 (버스번호) var busType: Int? // 버스 타입 var stopOrder: Int? // 순번 diff --git a/TMT/TMT/BusSearch/BusSearchViewModel.swift b/TMT/TMT/BusSearch/BusSearchViewModel.swift index 54a5487..c360004 100644 --- a/TMT/TMT/BusSearch/BusSearchViewModel.swift +++ b/TMT/TMT/BusSearch/BusSearchViewModel.swift @@ -11,7 +11,7 @@ final class BusStopSearchViewModel: ObservableObject { @Published var busStops: [BusStopInfo] = [] @Published var filteredBusStops: [BusStopInfo] = [] @Published var busNumbers = [Int]() - @Published var nameAndCoordinates: [BusStop] = [] + @Published var nameAndCoordinates: [BusStopInfo] = [] init() { loadCSV() @@ -75,23 +75,16 @@ final class BusStopSearchViewModel: ObservableObject { } func getNameAndCoordinates(busNum: String) { - var stopInfo: [BusStop] = [] + var stopInfo: [BusStopInfo] = [] for busStop in busStops { if busStop.busNumber == busNum { - if let stopName = busStop.stopName, + if let stopName = busStop.romanizedStopName, let xCoordinate = busStop.xCoordinate, let yCoordinate = busStop.yCoordinate { - stopInfo.append(BusStop(name: stopName, latitude: xCoordinate, longitude: yCoordinate)) + stopInfo.append(BusStopInfo(romanizedStopName: stopName, xCoordinate: xCoordinate, yCoordinate: yCoordinate)) } } } nameAndCoordinates = stopInfo } } - -struct BusStop: Identifiable { - let id = UUID() - let name: String - let latitude: String - let longitude: String -} diff --git a/TMT/TMT/MapView.swift b/TMT/TMT/MapView.swift index 88b6f57..62bc4a3 100644 --- a/TMT/TMT/MapView.swift +++ b/TMT/TMT/MapView.swift @@ -13,17 +13,15 @@ struct MapView: View { @StateObject var busStopSearchViewModel = BusStopSearchViewModel() // TODO: 사용자 위치 바뀌어도 화면 다시 업데이트 안되게 하기. + // 투두: 다시 테스트 해보고, 밑에 스트링 값 없을 때 대신 넣는거 생각해보기. var body: some View { ZStack { Map(coordinateRegion: $locationManager.region, showsUserLocation: true, annotationItems: busStopSearchViewModel.nameAndCoordinates) { stop in - MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: Double(stop.latitude) ?? 0, longitude: Double(stop.longitude) ?? 0)) { + MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: Double(stop.xCoordinate ?? "") ?? 0, longitude: Double(stop.yCoordinate ?? "") ?? 0)) { VStack { RoundedRectangle(cornerRadius: 5) .frame(width: 30, height: 30) .foregroundStyle(.blue) - Text(stop.name) - .font(.caption) - .foregroundColor(.black) } } } From b3fc4ef49b289d99bc6819f49b0ad8e8de3b4a26 Mon Sep 17 00:00:00 2001 From: Minkyeong-Choi Date: Fri, 18 Oct 2024 20:00:18 +0900 Subject: [PATCH 3/4] fix: #12 delete some comments --- TMT/TMT/MapView.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/TMT/TMT/MapView.swift b/TMT/TMT/MapView.swift index 62bc4a3..8b91874 100644 --- a/TMT/TMT/MapView.swift +++ b/TMT/TMT/MapView.swift @@ -12,8 +12,6 @@ struct MapView: View { @StateObject private var locationManager = LocationManager() @StateObject var busStopSearchViewModel = BusStopSearchViewModel() - // TODO: 사용자 위치 바뀌어도 화면 다시 업데이트 안되게 하기. - // 투두: 다시 테스트 해보고, 밑에 스트링 값 없을 때 대신 넣는거 생각해보기. var body: some View { ZStack { Map(coordinateRegion: $locationManager.region, showsUserLocation: true, annotationItems: busStopSearchViewModel.nameAndCoordinates) { stop in From 28a49cfabaea734018f0b95a1ef6186e20fca226 Mon Sep 17 00:00:00 2001 From: Minkyeong-Choi Date: Sat, 19 Oct 2024 00:17:31 +0900 Subject: [PATCH 4/4] fix: #12 delete getNameAndCoordinate function --- TMT/TMT/BusSearch/BusSearchViewModel.swift | 16 ---------------- TMT/TMT/MapView.swift | 6 +++--- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/TMT/TMT/BusSearch/BusSearchViewModel.swift b/TMT/TMT/BusSearch/BusSearchViewModel.swift index c360004..c04f6f4 100644 --- a/TMT/TMT/BusSearch/BusSearchViewModel.swift +++ b/TMT/TMT/BusSearch/BusSearchViewModel.swift @@ -11,7 +11,6 @@ final class BusStopSearchViewModel: ObservableObject { @Published var busStops: [BusStopInfo] = [] @Published var filteredBusStops: [BusStopInfo] = [] @Published var busNumbers = [Int]() - @Published var nameAndCoordinates: [BusStopInfo] = [] init() { loadCSV() @@ -58,7 +57,6 @@ final class BusStopSearchViewModel: ObservableObject { } return false } - fetchBusNumbersList() } @@ -73,18 +71,4 @@ final class BusStopSearchViewModel: ObservableObject { return nil })) } - - func getNameAndCoordinates(busNum: String) { - var stopInfo: [BusStopInfo] = [] - for busStop in busStops { - if busStop.busNumber == busNum { - if let stopName = busStop.romanizedStopName, - let xCoordinate = busStop.xCoordinate, - let yCoordinate = busStop.yCoordinate { - stopInfo.append(BusStopInfo(romanizedStopName: stopName, xCoordinate: xCoordinate, yCoordinate: yCoordinate)) - } - } - } - nameAndCoordinates = stopInfo - } } diff --git a/TMT/TMT/MapView.swift b/TMT/TMT/MapView.swift index 8b91874..936c960 100644 --- a/TMT/TMT/MapView.swift +++ b/TMT/TMT/MapView.swift @@ -14,7 +14,7 @@ struct MapView: View { var body: some View { ZStack { - Map(coordinateRegion: $locationManager.region, showsUserLocation: true, annotationItems: busStopSearchViewModel.nameAndCoordinates) { stop in + Map(coordinateRegion: $locationManager.region, showsUserLocation: true, annotationItems: busStopSearchViewModel.filteredBusStops) { stop in MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: Double(stop.xCoordinate ?? "") ?? 0, longitude: Double(stop.yCoordinate ?? "") ?? 0)) { VStack { RoundedRectangle(cornerRadius: 5) @@ -26,14 +26,14 @@ struct MapView: View { .edgesIgnoringSafeArea(.all) VStack { Button { - busStopSearchViewModel.getNameAndCoordinates(busNum: "207(기본)") + busStopSearchViewModel.searchBusStops(by: "207(기본)") } label: { Text("207번 버스") .background(.white) } Button { - busStopSearchViewModel.getNameAndCoordinates(busNum: "306(기본)") + busStopSearchViewModel.searchBusStops(by: "306(기본)") } label: { Text("472번 버스") .background(.white)