如何将带有标记的地图添加到 android 中的适配器项?

How to add map with marker to adapter item in android?

我在布局中添加了mapview。如何根据列表中的项目动态设置地图标记?

<com.google.android.gms.maps.MapView
                xmlns:map="http://schemas.android.com/apk/res-auto"
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                map:cameraZoom="15"
                map:liteMode="false"
                map:mapType="normal" />

您可以使用 Lat 和 Lng 将标记放置在所需位置。

fun addMarker(latLng: LatLng) {

val options = MarkerOptions().position(latLng)
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon))

map.addMarker(options)
}

onBindViewHolder 中为您的地图视图实施 OnMapReadyCallback 并覆盖 onMapReady()

像这样:

holder.mMapView.getMapAsync(new OnMapReadyCallback()
{
    @Override
    public void onMapReady(GoogleMap googleMap) {
        holder.mMapView = googleMap;

        if (holder.mMapView != null) {
            holder.mMapView.addMarker(...);
        }
    }
}