Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 谷歌地图显示的是一张空白地图_Android_Google Maps_Android Maps - Fatal编程技术网

Android 谷歌地图显示的是一张空白地图

Android 谷歌地图显示的是一张空白地图,android,google-maps,android-maps,Android,Google Maps,Android Maps,我目前遇到了这个问题:谷歌地图似乎根本没有加载。请参见下图: 这真的快把我逼疯了。我已经做了以下工作: 将我的包名+SHA1(调试密钥库)+SHA1(发布密钥库)添加到Google开发者控制台 已启用适用于Android的地图SDK。API密钥具有Android应用程序限制 从Firebase控制台将google-services.json添加到我的应用程序 添加了必要的权限: 将API密钥添加到清单文件-来自Google开发者控制台 添加了gms版本的元数据 这是我的片段的代码: impo

我目前遇到了这个问题:谷歌地图似乎根本没有加载。请参见下图:

这真的快把我逼疯了。我已经做了以下工作:

  • 将我的包名+SHA1(调试密钥库)+SHA1(发布密钥库)添加到Google开发者控制台
  • 已启用适用于Android的地图SDK。API密钥具有Android应用程序限制
  • 从Firebase控制台将google-services.json添加到我的应用程序
  • 添加了必要的权限:
  • 将API密钥添加到清单文件-来自Google开发者控制台
  • 添加了gms版本的元数据
  • 这是我的片段的代码:

    import android.content.Context;
    import android.content.pm.FeatureInfo;
    import android.content.pm.PackageManager;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.ErrorDialogFragment;
    import com.google.android.gms.common.GoogleApiAvailability;
    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.MapView;
    import com.google.android.gms.maps.MapsInitializer;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    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 MapsActivity extends Fragment implements OnMapReadyCallback {
    
        private MapFragment mapFragment;
        protected boolean readyToGo() {
            GoogleApiAvailability checker=
                    GoogleApiAvailability.getInstance();
    
            int status=checker.isGooglePlayServicesAvailable(getActivity());
    
            if (status == ConnectionResult.SUCCESS) {
                if (getVersionFromPackageManager(getActivity())>=2) {
                    return(true);
                }
                else {
                    Toast.makeText(getActivity(), "no maps", Toast.LENGTH_LONG).show();
    
                }
            }
            else if (checker.isUserResolvableError(status)) {
    
            }
            else {
                Toast.makeText(getActivity(),"no maps", Toast.LENGTH_LONG).show();
    
            }
    
            return(false);
        }
        private static int getVersionFromPackageManager(Context context) {
            PackageManager packageManager=context.getPackageManager();
            FeatureInfo[] featureInfos=
                    packageManager.getSystemAvailableFeatures();
            if (featureInfos != null && featureInfos.length > 0) {
                for (FeatureInfo featureInfo : featureInfos) {
                    // Null feature name means this feature is the open
                    // gl es version feature.
                    if (featureInfo.name == null) {
                        if (featureInfo.reqGlEsVersion != FeatureInfo.GL_ES_VERSION_UNDEFINED) {
                            return getMajorVersion(featureInfo.reqGlEsVersion);
                        }
                        else {
                            return 1; // Lack of property means OpenGL ES
                            // version 1
                        }
                    }
                }
            }
            return 1;
        }
        private static int getMajorVersion(int glEsVersion) {
            return((glEsVersion & 0xffff0000) >> 16);
        }
        public static MapsActivity newInstance() {
            MapsActivity fragment = new MapsActivity();
            return fragment;
        }
    
        public MapsActivity() {
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            readyToGo();
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.activity_maps, null, false);
    
            SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
    
            return view;
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            LatLng augsburg = new LatLng(48.348527, 10.915952);
    
            if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            googleMap.setMyLocationEnabled(true);
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(augsburg, 13));
    
            googleMap.addMarker(new MarkerOptions()
                    .title("Augsburg Zoo")
                    .snippet("Der coolste Zoo der Welt")
                    .position(augsburg));
        }
    }
    
    这是我的布局代码:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <TextView
            android:id="@+id/textView15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Coming soon!"
            android:textColor="@color/colorPrimary"
            android:textSize="30sp"
            android:visibility="invisible" />
    </RelativeLayout>
    
    请注意,当我对其他map项目使用相同的API_密钥时,它可以完美地工作。似乎地图没有显示在我的应用程序上,而是在其他项目上工作

    这些是我的家属:

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support:design:28.0.0-rc02'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'com.android.support:support-v4:28.0.0'
    
        implementation 'com.google.android.gms:play-services-maps:15.0.1'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
        implementation 'com.mikhaellopez:circularimageview:3.2.0'
        implementation 'gr.pantrif:easy-android-splash-screen:0.0.1'
        implementation 'com.github.GrenderG:Toasty:1.3.0'
        implementation 'com.github.adrielcafe:AndroidAudioRecorder:0.3.0'
        implementation 'com.google.firebase:firebase-core:16.0.1'
        implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5'
        implementation 'com.android.volley:volley:1.1.0'
        compile 'com.github.ybq:Android-SpinKit:1.1.0'
        implementation 'com.google.firebase:firebase-database:16.0.1'
        compile 'com.android.support:cardview-v7:28.0.0'
        compile 'com.github.bumptech.glide:glide:3.6.0'
        compile 'com.github.clans:fab:1.6.4'
        compile 'com.github.blikoon:QRCodeScanner:0.1.2'
        compile 'com.github.droidbyme:DroidDialog:c6decc7167'
        implementation 'com.android.support:multidex:1.0.3'
        compile project(':library')
    
    }
    apply plugin: 'com.google.gms.google-services'
    apply plugin: 'io.fabric'
    
    这是我的
    AndroidManifest.xml
    的内容:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.sample.x">
    
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.SEND_SMS" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.VIBRATE" />
    
    
    
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:largeHeap="true"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="MY_API_KEY" />
    
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="${applicationId}.com.vansuita.pickimage.provider"
                android:exported="false"
                android:grantUriPermissions="true"
                tools:replace="android:authorities">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/picker_provider_paths"
                    tools:replace="android:resource" />
            </provider>
    
            <activity
                android:name=".SplashscreenActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".MapsMarkerActivity"
                android:label="MAPS" />
            <activity
                android:name=".MapsActivity2"
                android:label="@string/title_activity_maps2"></activity>
        </application>
    
    </manifest>
    

    我昨天在公司设计客户时遇到了这个问题。和我身后的客户一起解决这个问题是疯狂的两天。我所做的是:

  • 从Google Dashboard API中删除当前键(API键)。 从Google dashboard API中删除firebase web密钥。创建一个 android的新密钥API,注册SHA-1和包名。和 加入android studio项目

  • 在FIrebase项目中,删除要再次添加的SHA-1代码。下载 google-services.json,并添加到项目中

  • 在发布模式下生成应用程序,在商店中发布,然后下载 在某些设备上,运行应用程序时应正常显示地图

  • 如果没有,它将在LogCat中显示错误,并说明您必须 注册以下SHA-1密钥:xxxxxxxxxxxxxxxx和包 名称:com.your.packagename

  • 只要在GoogleAPI中注册这个密钥,几分钟后就可以了 几秒钟,甚至不需要更新商店中的应用程序

  • <强>注:< /强>另一件事,考虑升级谷歌地图和谷歌服务的依赖关系。


    希望这能有所帮助。

    您尝试过这些方法吗

    启用多索引:

    注意:如果您的项目配置为MinSDK版本20或更低版本的multidex,并且您部署到运行Android 4.4(API级别20)或更低版本的目标设备,则Android Studio将禁用Instant Run

    禁用即时运行:


    尝试在您的onMapReady方法中添加这段代码

    MapsInitializer.initialize(Objects.requireNonNull(getContext()));
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.setMyLocationEnabled(true);
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(augsburg, 13));
    googleMap.addMarker(new MarkerOptions()
                .title("Augsburg Zoo")
                .snippet("Der coolste Zoo der Welt")
                .position(augsburg));
    
    有几件事需要注意

  • 正在检查运行时权限()-您的logcat显示其中一行的权限被拒绝
  • 尝试将应用程序上传到alfa或beta测试频道的playstore(以防止在应用程序通过所有测试之前访问目标用户)-除非您的应用程序已注册,否则某些API无法工作
  • 如果问题仍然存在,请尝试在firebase中删除并重新注册您的应用程序

  • 另外,请注意,谷歌地图API不再像以前一样免费提供。你需要订阅。更多信息请点击这里。 希望这有帮助


  • 在片段中使用map时,请更改代码,如下所示:-

      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
    
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.mapContainer, mMapFragment).commit();
        mMapFragment.getMapAsync(this);
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout 
          android:id="@+id/mapContainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
        />
    
    
        .....
    </RelativeLayout>
    
    将layout中的片段标记更改为FrameLayout,如下所示:-

      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
    
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.mapContainer, mMapFragment).commit();
        mMapFragment.getMapAsync(this);
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout 
          android:id="@+id/mapContainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
        />
    
    
        .....
    </RelativeLayout>
    

    我假设这可能是由于SHA 1指纹不匹配造成的。再次检查您是否使用相同的密钥库来获取SHA1

    或者让我们检查您的API密钥是否有效。为此,请添加以下MAP_API作为密钥

    AIzaSyDztCvpv3PVBxzWwY7mWqCMnGfDeOiGxwY
    
    将密钥直接添加到清单

    <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyDztCvpv3PVBxzWwY7mWqCMnGfDeOiGxwY" />
    
    
    
    在AndroidManifest.xml中添加以下权限(可选/无需)

    
    
    建立你的apk。并测试它

    如果地图显示,那么我们可以修复您的API密钥是问题所在

    否则

    可能是由于子片段管理器的原因,所以在视图页面中初始化片段,如下所示。然后测试它

     <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="yourpackage.MapsActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" />
    

    解决方案:

     I had the same problem in my App so you can try this open your Android studio and
     make sure you are login with the same user id of what you had created 
     `console.developer` account and after that you should do 
    
    
    
     SupportMapFragment mapFragment;
     GoogleMap mMap;
     Marker marker;
     LatLng origin = new LatLng(30.5595, 22.9375);
     LatLng dest = new LatLng(30.5595, 22.9375);
    
      @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.addMarker(new MarkerOptions()
                .position(origin)
                .title("title")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    
        googleMap.addMarker(new MarkerOptions()
                .position(dest));
    
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(origin, 15));
    }
    

    我有这个问题。在我的例子中,我更改了API密钥,一切都开始正常工作。检查logcat以了解问题。如果是相同的问题,则更改密钥,它应该可以工作。

    我做了一个简单的修复,Google Maps现在可以完美地工作了: 设置应用程序地图单击右上角的3点卸载所有更新。
    它将向您发出警告,地图将重置为出厂设置。忽略它,然后按Ok。重新启动手机。

    您的logcat怎么说?地图活动显示时提供logcat Show tour logcat应该有例外。我已经添加了logcat@Faizmir。您是否尝试过更新设备的播放服务版本?
     I had the same problem in my App so you can try this open your Android studio and
     make sure you are login with the same user id of what you had created 
     `console.developer` account and after that you should do 
    
    
    
     SupportMapFragment mapFragment;
     GoogleMap mMap;
     Marker marker;
     LatLng origin = new LatLng(30.5595, 22.9375);
     LatLng dest = new LatLng(30.5595, 22.9375);
    
      @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.addMarker(new MarkerOptions()
                .position(origin)
                .title("title")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    
        googleMap.addMarker(new MarkerOptions()
                .position(dest));
    
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(origin, 15));
    }