谷歌地图空白页与AndroidStudio



我是AndroidStudio的初学者,所以我尝试构建一个基本的应用程序。我添加了一个GoogleMap活动,效果很好。然后我把Firebase添加到应用程序中,我发现GoogleMapActivity已经不起作用了。它只是给我看了一个空白页面,左下角有"谷歌"标志。我试着在虚拟设备和真实设备上运行这个程序。没有成功。

我试图更改密钥,我也清理/重建了应用程序。事实是……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'
}

编辑1:添加我的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" />

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

将其添加到清单文件中

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

有关更多详细信息,请访问谷歌地图api

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

密钥库证书的SHA-1指纹

使用android studio:生成它的步骤

  1. 转到android工作室右侧的gradle文件
  2. 扩展您的项目
  3. 转到应用程序文件夹并点击android
  4. 选择要生成的签名报告。5( 您可以在底部的Run空间中找到它,关键字为SHA1

相关内容

  • 没有找到相关文章

最新更新