0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SwiftUI Mapkit Search

Posted at

view


var body: some View {
    HStack {
       Button {
            search(for: "playground")
       } label: {
            Label("Playgrounds", systemImage: "figure.and.child.holdinghands")
       }
       .buttonStyle(.borderedProminet)

        Button {
                    search(for: "playground")
               } label: {
                    Label("Playgrounds", systemImage: "figure.and.child.holdinghands")
       }
       .buttonStyle(.borderedProminet)
       
    }
    .labelStyle(.iconOnly)
}

search


@Binding var searchResults: [MKMapItem]

func search(for query: String, region: MKCoordinateRegion? = nil) {
    let request = MKLocalSearch.Request()
    request.naturalLanguageQuery = query
    request.resultTypes = .pointOfInterest

    if let region = region {
        request.region = region
    }
    Task {
        let search = MKLocalSearch(request: request)
        let response = try? await search.start()
        searchResults = response?.mapItems ?? []
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?