如何让 Google 地图上的标记标题自动同时显示?

How to make marker titles on Google Maps display all at once automatically?

我创建了一个使用 Google 地图的移动应用程序,但我似乎无法自动显示标记标题。我总是必须点击标记才能显示它的标题。有什么方法可以让标记标题出现在地图上后自动显示?我希望能够一次显示所有标记的标题。

像下面那样使用showInfoWindow()

Marker marker = mMap.addMarker(new MarkerOptions().position(currentPosition).title("Your text"));
marker.showInfoWindow();

当您在地图上添加标记时,在添加标记之后您必须使用 marker.showInfoWindow();

下面是打开 window 添加的示例标记:

Marker marker = myMap.addMarker(new MarkerOptions()
                     .position(latLng)
                     .title("Title")
                     .snippet("Snippet")
                     .icon(BitmapDescriptorFactory
                     .fromResource(R.drawable.marker)));

marker.showInfoWindow();

您可以使用 自定义 xml 布局 IconGenerator

val iconGenerator = IconGenerator(requireContext())

val markerView = LayoutInflater.from(requireContext()).inflate(R.layout.marker, null)
val textView = markerView.findViewById<TextView>(R.id.marker_title_text)

val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.ic_location_marker)

textView.text = "SomeText #1"

iconGenerator.setContentView(markerView)
iconGenerator.setBackground(drawable)

val markerOptions = MarkerOptions()
        .icon(BitmapDescriptorFactory.fromBitmap(iconGenerator.makeIcon()))
        .position(position)
        .anchor(iconGenerator.anchorU, iconGenerator.anchorV)

val marker = map?.addMarker(markerOptions)