メインコンテンツまでスキップ

Search for places by category

This example does not contain a UI. Results will be printed to the Xcode console.

ViewController
import UIKit
import MapboxSearch

class SimpleCategorySearchViewController: MapsViewController {
let searchEngine = CategorySearchEngine()
// let searchEngine = CategorySearchEngine(accessToken: "<#You can pass access token manually#>")

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

searchEngine.search(categoryName: "cafe") { (response) in
do {
let results = try response.get()
print("Number of category search results: \(results.count)")

for result in results {
print("\tResult coordinate: \(result.coordinate)")
}

self.showAnnotations(results: results)
} catch {
print("Error during category search: \(error)")
}
}
}
}
MapsViewController
import UIKit
import MapboxMaps
import MapboxSearch
import MapboxSearchUI

class MapsSearchViewController: UIViewController {
var searchController = MapboxSearchController()
var mapView: MapView?
var annotationManager: PointAnnotationManager?

override func viewDidLoad() {
super.viewDidLoad()

// Search setup
searchController.delegate = self
let panelController = MapboxPanelController(rootViewController: searchController)
addChild(panelController)

// Map setup
let mapView = MapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.mapView = mapView
view.addSubview(mapView)

annotationManager = mapView.annotations.makePointAnnotationManager()
}

func showResults(_ results: [SearchResult]) {
let annotations = results.map { searchResult -> PointAnnotation in
var annotation = PointAnnotation(coordinate: searchResult.coordinate)
annotation.textField = searchResult.name
annotation.textOffset = [0, -2]
annotation.textColor = ColorRepresentable(color: .red)
annotation.image = .default
return annotation
}

annotationManager?.syncAnnotations(annotations)
if case let .point(point) = annotations.first?.feature.geometry {
let options = CameraOptions(center: point.coordinates)
mapView?.mapboxMap.setCamera(to: options)
}
}
}

extension MapsSearchViewController: SearchControllerDelegate {
func categorySearchResultsReceived(results: [SearchResult]) {
showResults(results)
}

func searchResultSelected(_ searchResult: SearchResult) {
showResults([searchResult])
}

func userFavoriteSelected(_ userFavorite: FavoriteRecord) {
showResults([userFavorite])
}
}
このexampleは役に立ちましたか?