Skip to content

Commit

Permalink
feat/#8
Browse files Browse the repository at this point in the history
feat/#8 완료
  • Loading branch information
EvelynMacbookPro committed Aug 15, 2024
1 parent 5eabe6d commit a7d01ab
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
4 changes: 4 additions & 0 deletions MC2Maps.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
objects = {

/* Begin PBXBuildFile section */
A88329EE2C6DEBCC00D6CC87 /* MapLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = A88329ED2C6DEBCC00D6CC87 /* MapLocation.swift */; };
EAF2746D2C1C6B2C00D90AEA /* MC2MapsApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF2746C2C1C6B2C00D90AEA /* MC2MapsApp.swift */; };
EAF2746F2C1C6B2C00D90AEA /* MapsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF2746E2C1C6B2C00D90AEA /* MapsView.swift */; };
EAF274712C1C6B2E00D90AEA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = EAF274702C1C6B2E00D90AEA /* Assets.xcassets */; };
EAF274742C1C6B2E00D90AEA /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = EAF274732C1C6B2E00D90AEA /* Preview Assets.xcassets */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
A88329ED2C6DEBCC00D6CC87 /* MapLocation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapLocation.swift; sourceTree = "<group>"; };
EAF274692C1C6B2C00D90AEA /* MC2Maps.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MC2Maps.app; sourceTree = BUILT_PRODUCTS_DIR; };
EAF2746C2C1C6B2C00D90AEA /* MC2MapsApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MC2MapsApp.swift; sourceTree = "<group>"; };
EAF2746E2C1C6B2C00D90AEA /* MapsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapsView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -55,6 +57,7 @@
EAF5A4462C22D14600EA2907 /* Info.plist */,
EAF2746C2C1C6B2C00D90AEA /* MC2MapsApp.swift */,
EAF2746E2C1C6B2C00D90AEA /* MapsView.swift */,
A88329ED2C6DEBCC00D6CC87 /* MapLocation.swift */,
EAF274702C1C6B2E00D90AEA /* Assets.xcassets */,
EAF274722C1C6B2E00D90AEA /* Preview Content */,
);
Expand Down Expand Up @@ -141,6 +144,7 @@
files = (
EAF2746F2C1C6B2C00D90AEA /* MapsView.swift in Sources */,
EAF2746D2C1C6B2C00D90AEA /* MC2MapsApp.swift in Sources */,
A88329EE2C6DEBCC00D6CC87 /* MapLocation.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>MC2Maps.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
46 changes: 46 additions & 0 deletions MC2Maps/MapLocation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Maplocation.swift
// MC2Maps
//
// Created by Evelyn Hong on 8/15/24.
//

import SwiftUI
import CoreLocation

final class GeoServiceManager: NSObject, CLLocationManagerDelegate {
private var locationManager: CLLocationManager!

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

// 좌표 -> 도로명 주소
func getRoadAddress(for location: CLLocation, completion: @escaping (String?) -> Void) {
let geocoder = CLGeocoder()
let locale = Locale(identifier: "Ko-kr")
geocoder.reverseGeocodeLocation(location, preferredLocale: locale) { placeMarks, error in
guard let placeMarks = placeMarks,
let address = placeMarks.last,
error == nil else {
completion(nil) // 오류 발생 시 nil 반환
return
}
let country = address.country ?? ""
let administrativeArea = address.administrativeArea ?? ""
let locality = address.locality ?? ""
let subLocality = address.subLocality ?? ""
let subThoroughfare = address.subThoroughfare ?? ""
let fullAddress = "\(country) \(administrativeArea) \(locality) \(subLocality) \(subThoroughfare)"


DispatchQueue.main.async {
completion(fullAddress) // 주소 문자열을 반환
}
}
}
}
30 changes: 25 additions & 5 deletions MC2Maps/MapsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI
import MapKit
import CoreLocation


// 커스텀 어노테이션 구조체 생성
struct AnnotationItem: Identifiable, Equatable{
let id = UUID()
Expand Down Expand Up @@ -36,8 +37,12 @@ struct MapsView: View {
@State private var baseLocation : CLLocationCoordinate2D? // baseLocation을 현재위치로 사용

@State private var specialAnnotationCounter = 0//스페셜한 어노테이션 수
@State private var selectedSpecialAnnotation: AnnotationItem? // 선택된 어노테이션(선택시 업데이트;sheet를 통해 모달 올리기)
@State private var selectedSpecialAnnotation: AnnotationItem? = nil // 선택된 어노테이션(선택시 업데이트;sheet를 통해 모달 올리기)

@State private var roadAddress: String? = nil

let geoServiceManager = GeoServiceManager()

let maxAnnotations = 48 // 최대 어노테이션 수
let gridSize = 6
let cellSize = 0.0001
Expand All @@ -51,7 +56,7 @@ struct MapsView: View {
.resizable()
.frame(width: 25, height: 25)
.scaleEffect(selectedSpecialAnnotation == annotation ? 2.0 : 1.0)
.rotationEffect(Angle(degrees: selectedSpecialAnnotation == annotation ? 10 : 0))
.rotationEffect(Angle(degrees: selectedSpecialAnnotation == annotation ? 10 : 0))
.animation(.interpolatingSpring(mass: 2, stiffness: 80, damping: 10, initialVelocity: 0))
.onTapGesture {
withAnimation() {
Expand Down Expand Up @@ -111,6 +116,19 @@ struct MapsView: View {
}
Text("북: \(annotation.coordinate.latitude)")
Text("동: \(annotation.coordinate.longitude)")
// 도로명 주소 표시
if let roadAddress = roadAddress {
Text("주소: \(roadAddress)")
} else {
Text("주소를 불러오는 중...")
.onAppear {
let location = CLLocation(latitude: annotation.coordinate.latitude,
longitude: annotation.coordinate.longitude)
geoServiceManager.getRoadAddress(for: location) { address in
self.roadAddress = address
}
}
}
Text("오늘은 이곳에서 00만큼 머물렀습니다")
Button("Close") {
selectedSpecialAnnotation = nil
Expand Down Expand Up @@ -175,20 +193,22 @@ struct MapsView: View {
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
@Published var location: CLLocation?

override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.locationManager.allowsBackgroundLocationUpdates = true //백그라운드에서 동작

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
self.location = location
}


}

struct ContentView_Previews: PreviewProvider {
Expand Down

0 comments on commit a7d01ab

Please sign in to comment.