Google地图在Android活动中无法显示?

6
在我的Android应用程序中,我想要一个占据整个屏幕的Google地图的MainActivity。
我的问题是,Google地图没有显示地图,只有左下角的Google标志。

enter image description here

以下是应该显示地图的主要活动相关部分:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback,
        NavigationView.OnNavigationItemSelectedListener {

    private GoogleMap mMap;

    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        setupLayout();

        mFragmentManager = getSupportFragmentManager();
    }

    /**
     * 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));
    }

这是主活动内容的 XML,它包含 Google 地图片段:

<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="com.purringcat.stray.view.activity.MainActivity"/>

这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.purringcat.stray">

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

    <!--
         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. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:name=".Stray"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".view.activity.MainActivity"
            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=".view.activity.LoginActivity"
            android:label="@string/title_activity_login"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
        <activity android:name=".view.activity.SignUpActivity">
        </activity>
        <!--
             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"/>

    </application>

</manifest>

3
通常这是由于错误的地图集成(错误的令牌/密钥/证书)引起的。 - Vladyslav Matviienko
这是否与我没有使用FragmentActivity有关? - Tom Finet
  1. 确保您已在项目中添加了API密钥。
  2. 确保您的API在Google控制台上已启用。
  3. 确保您提供的SHA-1是正确的。
- Ronak Thakkar
FragmentActivity我认为它无关。我确定你正在使用错误的API密钥或为地图生成了错误的指纹。 - Vladyslav Matviienko
似乎api_key或地图集成方面没有任何问题。我真的很困惑,因为我似乎没有错误或漏洞。 - Tom Finet
你在发布文件夹中添加了Google地图密钥吗? - Pranav Ashok
12个回答

9

您需要在 Google 控制台中为您的项目启用 Google 地图 API。并且您还需要从控制台下载 JSON 文件。

1. 进入 Google 控制台

2. 点击您的项目。

3. 从左侧菜单面板中点击库部分。请参见下方图片

enter image description here

  1. 在Google Maps API中,点击Google Maps Android API

  2. 然后启用它。

  3. 现在进入Firebase控制台

  4. 点击你的项目,进入项目设置并下载JSON文件。

  5. 将该JSON文件添加到您的应用程序中。


如何启用地图? - Tom Finet
我在项目的应用文件夹中有google-services.json文件。 - Tom Finet
那么它应该能正常工作,你的代码是完美的。试着将appcompactActivity更改为FragementActivity。我不认为这会影响,但试一下吧。 - Vidhi Dave
我将活动更改为片段活动,但现在在此代码上出现错误:setSupportActionBar(toolbar); - Tom Finet
只需将其注释并运行一次,以查看它是否正常工作。 - Vidhi Dave
显示剩余2条评论

2

我刚刚查看了和你一样的谷歌地图。

只需将您的代码与我的进行比较。

MainActivity.java

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));
}

}

还有清单文件

<?xml version="1.0" encoding="utf-8"?>

<!--
     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. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    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>

注意:您需要在谷歌上创建一个项目

点击这里访问谷歌控制台

将您的密钥值替换google_maps_api中的内容

<resources>


<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Your Key</string>

希望这对你有帮助。 PS:你可以在Android Studio中生成Google地图活动。

1
将这些权限添加到清单文件中。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />

或者在Google控制台中检查API是否已启用

或者检查您的google_api_key


我已经包含了问题以包括清单。我已经启用了API并添加了以AIz开头的API密钥。 - Tom Finet

1

谷歌地图喜欢获取不同的活动生命周期状态。因此,您应该发送:map_view.onCreate(savedInstanceState)map_view.onResume()。如果发生以下情况,它还会喜欢onPause()onLowMemory()onSaveInstanceState(outState)onDestroy()


1

点击“限制”按钮,然后在API限制中->创建API时不要限制密钥。


1

将以下代码包含在AndroidManifest.xml文件中的application部分:

<uses-library
    android:name="org.apache.http.legacy"
    android:required="false" />

请查看文档 here

0

所有编码工作都已完成,请仔细检查。 然后必须对应用程序进行两个操作。

   1. add SHA -1 Key in Firebase console.
   2. Dowanload and add this json file in your code. 
       THis PC -> "Your project folder" -> app -> "here Put and Update json file"
   3. Go to Google cloud Console.
       API & Services -> Credentials -> Android key (auto created by Firebase)  -> copy "API Key". 

enter image description here

  4. put this API Key in Manifest file.

☻♥ 搞定了,保留代码。


0
我在将我的应用程序更新到Android-13后也遇到了类似的问题,但是我找到了解决办法。
在按照设置Google云项目的标准步骤后,我创建了API密钥,并通过将其限制为我的项目包和签名密钥指纹来确保其安全。
这里的主要问题是,最初我只添加了用于创建应用程序包的签名密钥的SHA-1指纹,但是忘记了添加在Google Play控制台中找到的Google生成的签名密钥(Google Play控制台 > 设置(在左侧抽屉菜单中)> 应用签名)。
一旦您将应用签名密钥证书SHA-1证书指纹添加到Android限制列表中的MAP_API_KEY中,地图就会开始加载。
希望这能帮到您。谢谢!

0
如果Activity正在打开,即使您提供了Google_map_key,地图仍然不可见,则对我有效的一个解决方案是通过复制google_maps_api.xml中的链接并粘贴到浏览器中创建新的Google地图API KEY。选择一个项目或创建一个项目,它将带您进入准备好Google API KEY的页面,将其复制并粘贴到指示所给出的位置即可。它会起作用。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接