Android谷歌地图显示空白屏幕

时间:2016-11-12 09:40:46

标签: java google-maps android-studio

我使用默认的谷歌地图启动android studio: enter image description here

在google_maps_api.xml中添加我的密钥,我确信路径: enter image description here

这是我的表现:

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

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.


    -->


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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


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

        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

主要活动:

package com.example.erfan.google;

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));
    }
}
我错了吗?尝试在堆栈中的另一篇文章中提出这么多建议但不适用于我:(仍然是空白的谷歌地图: enter image description here

3 个答案:

答案 0 :(得分:0)

首先,请确保在开发者控制台中启用Google Maps Android API以及您正在使用的所有API。此行为的另一个可能原因是使用不正确的SHA 1,因此请确保正确获取SHA 1指纹。检查此documentation有关如何正确获取API密钥和SHA 1指纹的信息。有关详细信息,请尝试按照tutorial关于如何使用Google Maps Android API进行操作。

  

注意:您必须在测试和发布中分配调试API密钥   发布APK时的API密钥。只需插入正确的API密钥,清理即可   重建你的项目应该没问题。建议卸载你的   重新加载前设备中的旧应用程序。

答案 1 :(得分:0)

发生在我身上。我刚刚来到这里:

https://console.developers.google.com/apis/

通过创建新项目(左窗格 - &gt; API管理)创建/激活/启用了另一个密钥。

他们会给你一把新钥匙。

它对我有用。

答案 2 :(得分:0)

您必须前往https://console.developers.google.com/apis/credentials并在您选择API Key后点击编辑(铅笔图标),然后转到页面下方 单击“添加包名称和SHA-1证书指纹” 然后添加此信息并保存

注意:设置最多可能需要5分钟才能生效

相关问题