カスタムビューでマーカーを作成する

ニューヨーク市の 3D 画像。エンパイア ステート ビルの上に 3 つのカスタム デジタル マーカーが浮かんでおり、そのうちの 1 つは「

MarkerView を使用すると、カスタムの Android View 階層を 3D マップのマーカーとしてレンダリングできます。カスタムビュー マーカーを使用すると、地理座標に固定されたリッチな UI レイアウト、インタラクティブなウィジェット、ビュー アニメーションを表示できます。

カスタムビュー マーカーを追加する

カスタム ビュー マーカーを地図に追加するには、MarkerViewOptions インスタンスを作成し、LatLngAltitude オブジェクトを使用して setPosition で位置を設定し、setContent を使用してカスタム Android View を指定します。次に、GoogleMap3D インスタンスの addMarkerView にオプションを渡します。

setContent にビューを指定しない場合、SDK はデフォルトの PinView を作成してレンダリングします。

// Add imports
import android.widget.TextView
import com.google.android.gms.maps3d.model.MarkerViewOptions
import com.google.android.gms.maps3d.model.latLngAltitude

// Create a custom Android View
val badgeView = TextView(context).apply {
    text = "Empire State Building"
    contentDescription = "Marker for Empire State Building"
    setPadding(24, 12, 24, 12)
}

// Configure MarkerViewOptions and add the marker view to the map
val options = MarkerViewOptions().apply {
    position = latLngAltitude {
        latitude = 40.7484
        longitude = -73.9857
        altitude = 100.0
    }
    content = badgeView
}

val markerView = googleMap3D.addMarkerView(options)

デフォルトの PinView をカスタマイズする

完全にカスタムの Android View を作成する代わりに、カスタムの PinConfiguration を使用して PinView を作成し(または setPinConfiguration を使用して既存のインスタンスを更新し)、それを setContent に渡すことで、カスタマイズされたピンをレンダリングできます。

// Add imports
import android.graphics.Color
import com.google.android.gms.maps3d.model.MarkerViewOptions
import com.google.android.gms.maps3d.model.PinView
import com.google.android.gms.maps3d.model.latLngAltitude
import com.google.android.gms.maps3d.model.pinConfiguration

// Create a customized PinView
val customPinView = PinView(context, pinConfiguration {
    backgroundColor = Color.BLUE
    borderColor = Color.WHITE
})

// Configure MarkerViewOptions and add the marker view to the map
val options = MarkerViewOptions().apply {
    position = latLngAltitude {
        latitude = 40.7484
        longitude = -73.9857
        altitude = 100.0
    }
    content = customPinView
}

val markerView = googleMap3D.addMarkerView(options)

インタラクションとアニメーションを処理する

MarkerView は Android の View をラップするため、getContent から返されるビューに、標準のクリック リスナーとビュー アニメーションを直接アタッチできます。

さらに、MarkerView は Anchorable を実装します。これにより、addPopover を使用して Popover をマーカービューに固定できます。

markerView?.content?.setOnClickListener { view ->
    // Animate the view when clicked
    view.animate().rotationBy(90f).setDuration(300).start()
}

高度と重なった場合の動作を構成する

MarkerView の垂直方向の位置と、重なり合う地図ラベルやマーカーとの相互作用をカスタマイズできます。

アクセシビリティと削除

setContent に提供されるカスタム Android View で、contentDescription などのユーザー補助プロパティを構成する必要があります。

地図から MarkerView を削除するには、remove を呼び出します。

markerView?.remove()