From ef9b833bb42c9876e53dfcfa4fbd72c30f7ad4be Mon Sep 17 00:00:00 2001 From: taek0622 Date: Fri, 2 Jun 2023 16:34:00 +0900 Subject: [PATCH] Feat:sparkles:: #32 Add AddressSearcher MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 주소 검색 기능을 담당하기 위한 AddressSearcher 클래스 추가 --- Nav.xcodeproj/project.pbxproj | 4 ++++ Nav/AddressSearcher.swift | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 Nav/AddressSearcher.swift diff --git a/Nav.xcodeproj/project.pbxproj b/Nav.xcodeproj/project.pbxproj index dcecd29..755c198 100644 --- a/Nav.xcodeproj/project.pbxproj +++ b/Nav.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 26A1500D29083A1300BC7355 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26A1500C29083A1300BC7355 /* ContentView.swift */; }; 26A1500F29083A1400BC7355 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 26A1500E29083A1400BC7355 /* Assets.xcassets */; }; 26A1501929083B7B00BC7355 /* MapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26A1501829083B7B00BC7355 /* MapView.swift */; }; + 26A82ED82A29CCDE00C23407 /* AddressSearcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26A82ED72A29CCDE00C23407 /* AddressSearcher.swift */; }; CE2D8D3829C59D5F00E5C104 /* ImagePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE2D8D3729C59D5F00E5C104 /* ImagePicker.swift */; }; CE2D8D3A29C59DF100E5C104 /* CategoryPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE2D8D3929C59DF100E5C104 /* CategoryPicker.swift */; }; DCE7EBCF2A172C1E00644745 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE7EBCE2A172C1E00644745 /* Extensions.swift */; }; @@ -37,6 +38,7 @@ 26A1500C29083A1300BC7355 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 26A1500E29083A1400BC7355 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 26A1501829083B7B00BC7355 /* MapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapView.swift; sourceTree = ""; }; + 26A82ED72A29CCDE00C23407 /* AddressSearcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddressSearcher.swift; sourceTree = ""; }; CE2D8D3729C59D5F00E5C104 /* ImagePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagePicker.swift; sourceTree = ""; }; CE2D8D3929C59DF100E5C104 /* CategoryPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryPicker.swift; sourceTree = ""; }; DCE7EBCE2A172C1E00644745 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; @@ -92,6 +94,7 @@ 26A1501029083A1400BC7355 /* Preview Content */, CE2D8D3729C59D5F00E5C104 /* ImagePicker.swift */, CE2D8D3929C59DF100E5C104 /* CategoryPicker.swift */, + 26A82ED72A29CCDE00C23407 /* AddressSearcher.swift */, ); path = Nav; sourceTree = ""; @@ -227,6 +230,7 @@ CE2D8D3829C59D5F00E5C104 /* ImagePicker.swift in Sources */, 26A1500D29083A1300BC7355 /* ContentView.swift in Sources */, 262C17C2290D090C00450E54 /* Font+.swift in Sources */, + 26A82ED82A29CCDE00C23407 /* AddressSearcher.swift in Sources */, 262C17C6290E385E00450E54 /* Text+.swift in Sources */, 262C17C0290D06F700450E54 /* Color+.swift in Sources */, FDFD7A6129289926001BE945 /* Bundle+Ext.swift in Sources */, diff --git a/Nav/AddressSearcher.swift b/Nav/AddressSearcher.swift new file mode 100644 index 0000000..50ae1e6 --- /dev/null +++ b/Nav/AddressSearcher.swift @@ -0,0 +1,31 @@ +// +// AddressSearcher.swift +// Nav +// +// Created by 김민택 on 2023/06/02. +// + +import Combine +import Foundation +import MapKit +import SwiftUI + +class AddressSearcher: NSObject, ObservableObject, MKLocalSearchCompleterDelegate { + @Published var searchQuery = "" + var completer: MKLocalSearchCompleter + @Published var completions: [MKLocalSearchCompletion] = [] + var cancellable: AnyCancellable? + + override init() { + completer = MKLocalSearchCompleter() + super.init() + cancellable = $searchQuery.assign(to: \.queryFragment, on: self.completer) + completer.delegate = self + } + + func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) { + self.completions = completer.results + } +} + +extension MKLocalSearchCompletion: Identifiable {}