Google地图未显示在片段中

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

[如果我发布的问题与许多其他问题相似,我很抱歉,但是每个问题都有不同的google map实现,并且尝试所有这些方法会完全破坏我的代码,并且由于我是Android开发的新手,所以我d如果您可以帮助我找出我的地图出了什么问题,不要冒险。谢谢。

当我运行我的应用程序时,我得到的是:enter image description here

这是我的Gradle.build(应用程序):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.binfo"
        minSdkVersion 27
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.0.0'
    implementation 'androidx.navigation:navigation-ui:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
//    implementation 'com.google.android.gms:play-services:12.0.1'

    implementation 'com.google.firebase:firebase-analytics:17.2.0'
    implementation 'com.google.firebase:firebase-firestore:21.1.1'
}


apply plugin: 'com.google.gms.google-services'

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.binfo" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/BINFO_GOOGLE_MAPS_API_KEY"/>

        <activity android:name="com.example.binfo.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">    <!--custom theme properties to be used for this activity-->

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>


</manifest>

我的片段xml:

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

    <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/frg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</RelativeLayout>

和我的片段Java:

package com.example.binfo;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class HomeFragment extends Fragment {

    public void mapFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.frg);  //use SuppoprtMapFragment for using in fragment instead of activity  MapFragment = activity   SupportMapFragment = fragment
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                mMap.clear(); //clear old markers

                CameraPosition googlePlex = CameraPosition.builder()
                        .target(new LatLng(37.4219999,-122.0862462))
                        .zoom(10)
                        .bearing(0)
                        .tilt(45)
                        .build();

                mMap.animateCamera(CameraUpdateFactory.newCameraPosition(googlePlex), 10000, null);

                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(37.4219999, -122.0862462))
                        .title("Spider Man")
                        .icon(bitmapDescriptorFromVector(getActivity(),R.drawable.spider)));

                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(37.4629101,-122.2449094))
                        .title("Iron Man")
                        .snippet("His Talent : Plenty of money"));

                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(37.3092293,-122.1136845))
                        .title("Captain America"));
            }
        });


        return rootView;
    }

    private BitmapDescriptor bitmapDescriptorFromVector(Context context, int vectorResId) {
        Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId);
        vectorDrawable.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());
        Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        vectorDrawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }
}

如评论所建议,这是我的完整日志:

9390-19390/? I/m.example.binf: Not late-enabling -Xcheck:jni (already on)
2019-10-17 07:42:51.772 19390-19390/? W/m.example.binf: Unexpected CPU variant for X86 using defaults: x86
2019-10-17 07:42:52.292 19390-19390/com.example.binfo I/m.example.binf: The ClassLoaderContext is a special shared library.
2019-10-17 07:42:53.768 19390-19390/com.example.binfo V/FA: Registered activity lifecycle callback
2019-10-17 07:42:53.770 19390-19390/com.example.binfo I/FirebaseInitProvider: FirebaseApp initialization successful
2019-10-17 07:42:53.974 19390-19390/com.example.binfo V/FA: onActivityCreated
2019-10-17 07:42:54.032 19390-19390/com.example.binfo W/m.example.binf: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-10-17 07:42:54.032 19390-19390/com.example.binfo W/m.example.binf: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-10-17 07:42:54.206 19390-19390/com.example.binfo D/OpenGLRenderer: Skia GL Pipeline
2019-10-17 07:42:54.376 19390-19427/com.example.binfo V/FA: Collection enabled
2019-10-17 07:42:54.377 19390-19427/com.example.binfo V/FA: App package, google app id: com.example.binfo, 1:1010647557505:android:97517c54af5adf8581b480
2019-10-17 07:42:54.401 19390-19427/com.example.binfo I/FA: App measurement is starting up, version: 18202
2019-10-17 07:42:54.401 19390-19427/com.example.binfo I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2019-10-17 07:42:54.401 19390-19427/com.example.binfo I/FA: To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app com.example.binfo
2019-10-17 07:42:54.401 19390-19427/com.example.binfo D/FA: Debug-level message logging enabled
2019-10-17 07:42:54.412 19390-19390/com.example.binfo I/zzbz: Making Creator dynamically
2019-10-17 07:42:54.422 19390-19390/com.example.binfo W/m.example.binf: Unsupported class loader
2019-10-17 07:42:54.426 19390-19390/com.example.binfo W/m.example.binf: Skipping duplicate class check due to unsupported classloader
2019-10-17 07:42:54.429 19390-19390/com.example.binfo I/DynamiteModule: Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:221
2019-10-17 07:42:54.429 19390-19390/com.example.binfo I/DynamiteModule: Selected remote version of com.google.android.gms.maps_dynamite, version >= 221
2019-10-17 07:42:54.429 19390-19390/com.example.binfo V/DynamiteModule: Dynamite loader version >= 2, using loadModule2NoCrashUtils
2019-10-17 07:42:54.429 19390-19430/com.example.binfo W/DynamiteModule: Local module descriptor class for providerinstaller not found.
2019-10-17 07:42:54.462 19390-19390/com.example.binfo W/m.example.binf: Unsupported class loader
2019-10-17 07:42:54.464 19390-19430/com.example.binfo I/DynamiteModule: Considering local module providerinstaller:0 and remote module providerinstaller:0
2019-10-17 07:42:54.466 19390-19430/com.example.binfo W/ProviderInstaller: Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
2019-10-17 07:42:54.503 19390-19390/com.example.binfo W/m.example.binf: Skipping duplicate class check due to unsupported classloader
2019-10-17 07:42:54.539 19390-19430/com.example.binfo I/m.example.binf: The ClassLoaderContext is a special shared library.
2019-10-17 07:42:54.553 19390-19390/com.example.binfo I/Google Maps Android API: Google Play services client version: 12451000
2019-10-17 07:42:54.556 19390-19430/com.example.binfo I/m.example.binf: The ClassLoaderContext is a special shared library.
2019-10-17 07:42:54.557 19390-19430/com.example.binfo I/m.example.binf: The ClassLoaderContext is a special shared library.
2019-10-17 07:42:54.567 19390-19390/com.example.binfo I/Google Maps Android API: Google Play services package version: 19420040
2019-10-17 07:42:54.580 19390-19429/com.example.binfo W/m.example.binf: Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
2019-10-17 07:42:54.695 19390-19430/com.example.binfo V/NativeCrypto: Registering com/google/android/gms/org/conscrypt/NativeCrypto's 284 native methods...
2019-10-17 07:42:54.716 19390-19427/com.example.binfo V/FA: Connecting to remote service
2019-10-17 07:42:54.723 19390-19403/com.example.binfo I/m.example.binf: Background concurrent copying GC freed 8422(977KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 2MB/4MB, paused 865us total 114.508ms
2019-10-17 07:42:54.805 19390-19430/com.example.binfo W/m.example.binf: Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (light greylist, reflection)
2019-10-17 07:42:54.811 19390-19427/com.example.binfo V/FA: Connection attempt already in progress
2019-10-17 07:42:54.914 19390-19430/com.example.binfo D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2019-10-17 07:42:54.920 19390-19430/com.example.binfo I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL
2019-10-17 07:42:55.027 19390-19427/com.example.binfo V/FA: Activity resumed, time: 29685848
2019-10-17 07:42:55.055 19390-19427/com.example.binfo I/FA: Tag Manager is not found and thus will not be used
2019-10-17 07:42:55.087 19390-19427/com.example.binfo D/FA: Logging event (FE): screen_view(_vs), Bundle[{ga_event_origin(_o)=auto, ga_screen_class(_sc)=MainActivity, ga_screen_id(_si)=-3010706966078322650}]
2019-10-17 07:42:55.149 19390-19459/com.example.binfo I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-10-17 07:42:55.149 19390-19459/com.example.binfo I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2019-10-17 07:42:55.149 19390-19459/com.example.binfo I/OpenGLRenderer: Initialized EGL, version 1.4
2019-10-17 07:42:55.149 19390-19459/com.example.binfo D/OpenGLRenderer: Swap behavior 1
2019-10-17 07:42:55.150 19390-19459/com.example.binfo W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-10-17 07:42:55.150 19390-19459/com.example.binfo D/OpenGLRenderer: Swap behavior 0
2019-10-17 07:42:55.160 19390-19459/com.example.binfo D/EGL_emulation: eglCreateContext: 0xf0b09560: maj 3 min 0 rcv 3
2019-10-17 07:42:55.177 19390-19459/com.example.binfo D/EGL_emulation: eglMakeCurrent: 0xf0b09560: ver 3 0 (tinfo 0xf0b31b20)
2019-10-17 07:42:55.441 19390-19459/com.example.binfo D/EGL_emulation: eglMakeCurrent: 0xf0b09560: ver 3 0 (tinfo 0xf0b31b20)
2019-10-17 07:42:55.449 19390-19456/com.example.binfo D/EGL_emulation: eglCreateContext: 0xec3062c0: maj 1 min 0 rcv 1
2019-10-17 07:42:55.477 19390-19427/com.example.binfo V/FA: Connection attempt already in progress
2019-10-17 07:42:55.493 19390-19456/com.example.binfo D/EGL_emulation: eglMakeCurrent: 0xec3062c0: ver 1 0 (tinfo 0xec303970)
2019-10-17 07:42:55.547 19390-19427/com.example.binfo V/FA: Connection attempt already in progress
2019-10-17 07:42:55.626 19390-19427/com.example.binfo D/FA: Connected to remote service
2019-10-17 07:42:55.634 19390-19427/com.example.binfo V/FA: Processing queued up service tasks: 4
2019-10-17 07:42:57.022 19390-19457/com.example.binfo W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
2019-10-17 07:42:57.030 19390-19457/com.example.binfo I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
2019-10-17 07:42:57.031 19390-19457/com.example.binfo I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 4
2019-10-17 07:42:57.035 19390-19457/com.example.binfo W/m.example.binf: Unsupported class loader
2019-10-17 07:42:57.036 19390-19457/com.example.binfo W/m.example.binf: Skipping duplicate class check due to unsupported classloader
2019-10-17 07:43:01.081 19390-19427/com.example.binfo V/FA: Inactivity, disconnecting from the service
2019-10-17 07:44:30.231 19390-19474/com.example.binfo W/ManagedChannelImpl: [{0}] Failed to resolve name. status={1}
java android google-maps android-studio maps
2个回答
0
投票

在您的片段中使用MapView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

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

</RelativeLayout>

在您的片段中使用:

  public class HomeFragment extends Fragment implements OnMapReadyCallback {

    private GoogleMap mMap;
    private MapView mMapView;

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View rootView = inflater.inflate(R.layout.fragment_home, container, false)

             /*
             * Configure Mapview and sync to google map.
             */
            mMapView = rootView.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);
            mMapView.getMapAsync(this);
            mMapView.onResume(); // needed to get the map to display immediately

             //your code
            return rootView;
    }

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

它将正常工作。另外,请检查您的地图密钥和互联网连接。


0
投票

根据您的代码,我没有发现任何错误,但是根据您的屏幕截图,看来您的互联网连接丢失了,因此,请提供适当的互联网连接并先检查一下是否可以。

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