android - 谷歌地图 fragment 出现但没有 map

标签 android google-maps android-fragments android-fragmentactivity

我在 fragment 中使用 map ,但如下所示 screenshot , map 没有出现。我确定我有互联网连接和 GPS 信号,所以我认为这不是连接问题。

这是我的 fragment 中的代码:

public class MapViewFragment extends Fragment {

    MapView mMapView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.location_fragment, container, false);

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mMapView = rootView.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume(); // needed to get the map to display immediately

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {

                googleMap = mMap;

                googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

                // For showing a move to my location button
                googleMap.setMyLocationEnabled(true);

                // For dropping a marker at a point on the Map
                LatLng sydney = new LatLng(-34, 151);
                googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));

                // For zooming automatically to the location of the marker
                CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            }
        });

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}

这是 fragment 的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

</RelativeLayout>

然后我使用以下过程将这个 fragment 插入到我的主要 Activity 布局中:

public void addFragment(Fragment fragment, boolean addToBackStack, String tag) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();

        if (addToBackStack) {
            ft.addToBackStack(tag);
        }
        ft.replace(R.id.container_frame_back, fragment, tag);
        ft.commitAllowingStateLoss();
    }

我的 MainActivity 的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Fragment 1 " />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/container_frame_back"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="56dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

所以我真的不明白为什么我没有显示 map ,我也不认为这是权限问题,我已经在以前的项目中使用过 map ,但这次它只是出于某种原因没有显示...感谢您的任何回复!

最佳答案

问题已解决,事实证明,当您在外部设备上测试您的应用程序时,与权限相关的错误消息不会显示在日志中。我的 map fragment 中的权限有问题。我刚刚添加了以下代码,它工作得很好:

    String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION};
    requestPermissions(perms, 0); // Use this method in Fragment classes

关于android - 谷歌地图 fragment 出现但没有 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59273295/

相关文章:

android - 如何使用 Android Google map 应用程序的双击标记打开新 Activity ?

android - 有时 fragment 未附加到 Activity (onCreateOptionsMenu)

android - DialogFragment getActivity() 在 fragment 处理其他数据后返回 null

Android:用另一个 View 替换一个 View

Android - 当用户撤销权限时,Facebook 错误不会调用授权

c# - 如何在Gmap.Net中显示车辆移动方向

android - java.lang.RuntimeException : android. os.TransactionTooLargeException:在 fragment 之间导航时数据包大小 558780 字节

java - 存在不同情况的资源,并且不生成类文件

android - 如何在 ionic 框架中获取用户数据(公共(public)资料和电子邮件)

android - 如何将我的数据库的 AsyncTask 与 android 中的谷歌地图结合起来?