AndroidStudio的GoogleMaps空白页

时间:2018-07-21 04:58:18

标签: android google-maps android-studio google-api

我是AndroidStudio的初学者,所以我尝试构建一个基本应用。我添加了GoogleMap活动,效果很好。 然后,我将Firebase添加到了应用程序中,然后我发现GoogleMapActivity不再起作用。它只是向我显示一个空白页面,左下角带有“ google”徽标。 我试图在虚拟设备和真实设备上运行该程序。没有成功。

我试图更改密钥,我也清理/重建了该应用程序。 事实是...。logcat没有显示任何消息,甚至没有警告。

我真的不明白为什么GoogleMapActivity以前可以工作,现在不再工作。

您必须知道,当我添加Firebase时,我改变了很多依赖项。也许是SDK版本控制的问题?我不知道...

这是我在AndroidManifest.xml中的权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>


    <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.barcodelibrary.permission.MAPS_RECEIVE"/>

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_key" />
<meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

当然,我在 google_maps_api.xml 文件中有一个类似于以下内容的有效Google API密钥。

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">[i have a real key but i don't know if i can show it here ?]</string>

这是我的 build.gradle(模块:应用)文件:

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

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.kandel.myandroidapp"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:support-annotations:28.0.0-alpha3'
    implementation 'com.google.firebase:firebase-auth:16.0.2'
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-database:16.0.1'

    implementation 'com.google.android.gms:play-services:12.0.1'
    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    implementation 'com.google.android.gms:play-services-auth:15.0.1'
    implementation 'com.google.android.gms:play-services-identity: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'
}

EDIT1:添加我的GoogleMapActivity代码

如您所见,这是AndroidStudio提供的基本活动。 我想念什么吗?

package com.kandel.myandroidapp;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

编辑2:添加我的Activity_maps.xml文件

<?xml version="1.0" encoding="utf-8"?>
<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="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" />

3 个答案:

答案 0 :(得分:0)

添加Firebase时,必须将其google-services.json添加到项目中。将Firebase添加到项目后,Google Map在该文件内使用密钥

答案 1 :(得分:0)

将此添加到清单文件中

 ` <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="Your Api key" />`

有关更多详细信息,请访问google map api

答案 2 :(得分:0)

您应该在生成密钥时添加SHA-1指纹,您将从Android Studio或命令行中获取它。您可以通过以下链接来生成它,并在生成地图密钥时使用它。

SHA-1 fingerprint of keystore certificate

使用android studio生成它的步骤:

  1. 转到android studio右侧的gradle文件。
  2. 扩展项目
  3. 转到应用程序文件夹,然后单击android
  4. 选择签名报告以生成报告。 5)您可以在底部的“运行”空间中使用关键字SHA1来找到它