【问题标题】:GoogleMaps blank page with AndroidStudio带有 Android Studio 的谷歌地图空白页
【发布时间】:2018-12-29 09:08:03
【问题描述】:

我是 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 (module:app) 文件:

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" />

【问题讨论】:

  • 您需要在清单中添加&lt;meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="Your key" /&gt;

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


【解决方案1】:

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

SHA-1 fingerprint of keystore certificate

使用 android studio 生成它的步骤:

  1. 进入android studio右侧的gradle文件。
  2. 扩展您的项目
  3. 进入app文件夹,点击android
  4. 选择签名报告以生成它。 5)你可以在底部的Run空间找到它,关键字为SHA1

【讨论】:

    【解决方案2】:

    将此添加到您的清单文件中

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

    更多详情请访问google map api

    【讨论】:

    • 哦,对不起,我只是忘了在帖子上添加它!但是我已经在我的程序中得到了这条线。我编辑我的帖子,以便您可以看到
    • 发布您的 MapsActivity xml
    • 完成。如您所见,它是来自 AndroidStudio 的基本文件
    • 检查您是否在开发者控制台中启用了 api 密钥并检查 SHA-1
    • 我是初学者,所以... SHA-1 是什么?
    【解决方案3】:

    当您添加 firebase 时,您必须将其 google-services.json 添加到项目中。将firebase添加到项目后,谷歌地图使用该文件中的密钥

    【讨论】:

    • 我已经添加了。 Firebase 运行良好,这不是重点。问题在于 GoogleMap
    • @HugoKandel 好的。也许那是因为` implementation 'com.google.android.gms:play-services:12.0.1'` 使用 15.0.1 而不是 12.0.1 版本
    • 是的,这是因为 12.0.1 是可能的“最高”版本。 15.0.1 版本的“play-service”不存在。它与 play-services-map 不同,后者具有 15.0.1
    • 你能解释一下为什么你需要播放服务,因为当你添加它的孩子时,你不需要添加核心库
    • @MRah 我用我的 GoogleMapActivity.java 文件编辑帖子。
    猜你喜欢
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2013-05-16
    • 1970-01-01
    • 2017-08-16
    • 2020-06-06
    相关资源
    最近更新 更多