오직 장소 검색 기능을 위해 사용한 MapKit


MKLocalSearchstart(completionHandler:) 를 활용해 장소를 검색하고, 결과를 [MKMapItem] 형태로 받아올 수 있다.

아래는 공식문서에 소개되어있는 검색 예제

let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = "coffee"

// Set the region to an associated map view's region.
searchRequest.region = myMapView.region

let search = MKLocalSearch(request: searchRequest)
search.start { (response, error) in
    guard let response = response else {
        // Handle the error.
    }
    
    for item in response.mapItems {
        if let name = item.name,
            let location = item.placemark.location {
            print("\\(name): \\(location.coordinate.latitude),\\(location.coordinate.longitude)")
        }
    }
}

async throws 형태의 메서드도 지원하지만 네트워크 관련해서 아직은 팀에서 async 도입에 대해 논의해보지 않았으므로 completion handler가 있는 메서드로 “회귀”했다.

트러블슈팅


Region 설정에서 발생한 문제 (메모리 릭, 검색 범위)

장소 검색의 근간이 되는 맵뷰가 존재하고, 해당 범위에서만 검색하고 싶을 때 지역을 설정한다..는 것 같다. 처음엔 무조건 region도 설정해줘야 동작하는 줄 알고 사용하지도 않는 맵뷰를 만들어서 해당 맵뷰의 region으로 일단 설정했는데 아래와 같은 경고가 떴다.

[Memory] Resetting zone allocator with allocations still alive

스크린샷 2022-11-15 오후 9.09.07.png

Resetting zone allocator with allocations still alive

request.region = MKMapView().region // 이런 느낌으로 되어 있던 걸
request.region = MKCoordinateRegion(MKMapRect.world) // 이렇게 수정함