与地图注点互动

照片显示,点按后,屏幕底部会显示一个包含地点详情的小弹出式窗口。

当用户点按 Map上的地图注点 (POI) 时, Maps 3D SDK for iOS (GoogleMaps3DKit) 会显示 该地图注点的地点微件。您可以使用onTap(_:)、onPlaceTap(_:)和placeInformationEnabled(_:)监听点按事件,并控制是否 显示此 widget。

监听点按事件

如需监听 Map上的点按事件,请应用 onTap(_:) 修饰符。

当用户点按地图时,闭包会收到一个 MapTapInfo 对象,其中包含以下属性:

控制 Places UI widget

如需控制在点按地点时是否显示内置 Places UI widget,请将 onPlaceTap(_:) 应用于 Map。闭包会收到点按的 placeId (String) 并返回 PlaceTapAction:

  • 返回 .default:显示内置 Places UI widget。
  • Return .none:禁止显示内置地点 UI 微件。

以下示例监听了点按事件并禁止显示默认 Places UI widget:

import GoogleMaps3DKit
import SwiftUI

struct ContentView: View {
  @State private var camera = Camera(center: .init(latitude: 37.7749, longitude: -122.4194))

  var body: some View {
    Map(camera: $camera, mode: .hybrid)
      .onPlaceTap { placeId in
        // Suppress the built-in Places UI widget
        .none
      }
      .onTap { tapInfo in
        switch tapInfo.content {
        case .map:
          print("Tapped map at \(tapInfo.location)")
        case .place(let placeId):
          print("Tapped place \(placeId) at \(tapInfo.location)")
        }
      }
  }
}

如需针对 Map上的所有地图注点点按操作停用内置地点信息卡片,请将 false传递给placeInformationEnabled(_:):

import GoogleMaps3DKit
import SwiftUI

struct ContentView: View {
  var body: some View {
    Map(mode: .hybrid)
      .placeInformationEnabled(false)
  }
}