場所検索を使用する
マップに場所検索機能を追加する
まず、場所検索を設定または既存のAmazon Location Serviceリソースを使用の指示に従って検索インデックスリソースをプロビジョニングして、アプリを設定してください。また、アプリケーションでマップの表示をすでに設定していることを確認してください。
AMLMapViewに場所検索UIコンポーネントを追加するには、目的のレイアウトでViewにAMLSearchBarを追加します。検索時に、Geo.PlaceはAmplifyMapLibre.createFeatures(places)を使用してMGLPointFeatureに変換されます。最後に、それらの変換されたMGLPointFeatureをmapState.featuresに割り当てます。別の方法として、AMLSearchBarを含む他の事前設定されたUIコンポーネントを含むAMLMapCompositeViewを直接活用することもできます。
import SwiftUIimport AmplifyMapLibreUIimport AmplifyMapLibreAdapterimport Amplify
struct MyMapView: View {
@StateObject private var mapState = AMLMapViewState() @State private var searchText = "" @State private var displayState: AMLSearchBar.DisplayState = .map
var body: some View { ZStack(alignment: .top) { AMLMapView(mapState: mapState) .edgesIgnoringSafeArea(.all)
AMLSearchBar( text: $searchText, displayState: $displayState, onEditing: { }, onCommit: search, onCancel: { mapState.features = [] } ) .padding() } }
private func search() { let searchArea = Geo.SearchArea.near(mapState.center) let searchOptions = Geo.SearchForTextOptions(area: searchArea) Task { do { let places = try await Amplify.Geo.search(for: searchText, options: searchOptions) await MainActor.run { self.mapState.features = AmplifyMapLibre.createFeatures(places) } } catch let error as Geo.Error { print("Failed to search: \(error)") } catch { print("Unexpected error: \(error)") } } }}フィーチャーアイコンをカスタマイズする
AMLMapViewまたはAMLMapCompositeViewに表示されるフィーチャー画像をカスタマイズするには、featureImage()ビュー修飾子を活用できます。
var body: some View { AMLMapView(mapState: mapState) .featureImage { let image = UIImage( systemName: "paperplane.circle.fill", withConfiguration: UIImage.SymbolConfiguration( font: .systemFont(ofSize: 22, weight: .medium) ) )! return image } .edgesIgnoringSafeArea(.all)}場所ベースの検索機能
Amplify Geoを使用すると、テキスト、住所、または地理座標で場所を検索できます。
テキストを検索する
Amplify.Geo.search(for text:) APIを使用すると、住所、名前、都市、地域などの自由形式のテキストで場所またはポイントオブインタレストを検索できます。
do { let places = try await Amplify.Geo.search(for: "coffee shops") dump(places)} catch { print(error)}Geo.SearchForTextOptions内で以下のパラメータを指定して検索を改善できます
area.near- 検索の開始場所として機能します。.within- 検索内の領域を制限します。
countries- 検索結果を指定された国に制限します。maxResults- 最大結果セットを制限します(デフォルトは50)。
let coordinates = Geo.Coordinates(latitude: 47.62246, longitude: -122.336775)let options = Geo.SearchForTextOptions(area: .near(coordinates), countries: [.usa, .can], maxResults: 25)
do { let places = try await Amplify.Geo.search(for: "coffee shops", options: options) dump(places)} catch let error as Geo.Error { print("Failed to search: \(error)"} catch { print("Unexpected error: \(error)")}座標を検索する
Amplify.Geo.search(for coordinates:) APIは逆ジオコーダーで、座標点を取得し、マップのその点で見つかるものについての情報を返します。
do { let places = try await Amplify.Geo.search(for: coordinates) dump(places)} catch let error as Geo.Error { print("Failed to search: \(error)")} catch { print("Unexpected error: \(error)")}Geo.SearchForCoordinatesOptions内で以下のパラメータを指定して検索を改善できます
maxResults- 最大結果セットを制限します(デフォルトは50)
let options = Geo.SearchForCoordinatesOptions(maxResults: 25)