[Google Map返回空对象引用,当在Android中的recylerview上实现地图时

问题描述 投票:1回答:1

我正在recylerview适配器上实现google map,但它返回空对象引用。

尝试调用虚拟方法'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions )'(在空对象引用上)

public class TripAdapterViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
        TripAdapterBinding tripAdapterBinding;
        MapView mapView;
        GoogleMap gMap;

        TripAdapterViewHolder(TripAdapterBinding tripAdapterBinding) {
            super(tripAdapterBinding.itemTrip);
            this.tripAdapterBinding = tripAdapterBinding;
            setMap(tripAdapterBinding.map);
        }

        public void setMap(MapView map) {
            mapView = map;
            if (mapView != null)
            {
                mapView.onCreate(null);
                mapView.onResume();
                mapView.getMapAsync(this);
            }
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            this.gMap = googleMap;
        }

[我叫我的持有人在地图上显示数据

@Override
    public void onBindViewHolder(@NonNull TripAdapter.TripAdapterViewHolder holder, int position) {

        try {
            GoogleMap thisMap = holder.gMap;

            LatLng startPoint = new LatLng(37.7750, 122.4183);

            thisMap.addMarker(new MarkerOptions()
                    .position(startPoint)
                    .title("")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

            thisMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startPoint, 12));



        }catch (Exception e){
            Log.e("MapException "," "+e.getMessage());
        }
        holder.bindTrip(tripLists.get(position));
    }
android google-maps mvvm android-databinding
1个回答
1
投票

您应该通过显示路径来使用静态地图。

Static map image generate在这里,您将获得地图图像,可用于在回收站视图适配器中显示。

© www.soinside.com 2019 - 2024. All rights reserved.