首页 > 解决方案 > MapView 未正确显示

问题描述

我在androidMapView里面使用。RecycleView

这是我的xml代码:

<com.google.android.gms.maps.MapView
                android:id="@+id/map_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"> 

和:

private fun initializeMap() {

            val mapView : MapView = binding.root.findViewById(R.id.map_view)
            mapView?.let {
                mapView.onCreate(null)
                mapView.getMapAsync(this)
            }
        }

override fun onMapReady(map: GoogleMap?) {
            MapsInitializer.initialize(fragment.context)
            googleMap = map
            googleMap?.let {

                it.setOnMapLoadedCallback { this }
                it.setOnMapClickListener { this }
                it.uiSettings.setAllGesturesEnabled(false)
                it.uiSettings.isZoomControlsEnabled = false
            }
        }

        override fun onMapLoaded() {

        }


        override fun onMapClick(p0: LatLng?) {

        }

出现的地图如下

在此处输入图像描述

tp 需要很长时间才能出现,也没有直接出现,我滚动了很多

还有方法onMapLoaded()并且onMapClick()不调用

标签: androidandroid-mapview

解决方案


这是我的 map.xml 文件

 <com.google.android.gms.maps.MapView android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />

这是java类

public class map extends Fragment implements OnMapReadyCallback {
GoogleMap mMap;
private MapView mapView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.map,null);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
  @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
 LatLng ahmedabad = new LatLng(23.022505, 72.571365);
        BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.icons8fiat50060);

    MarkerOptions markerOptions = new MarkerOptions().position(ahmedabad)
            .title("Current Location")
            .snippet("Thinking of finding some thing...")
            .icon(icon);

     googleMap.addMarker(markerOptions);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ahmedabad,30f));
  }

这很容易,我认为这对您有帮助。


推荐阅读