Android上的Google地图未显示任何内容

时间:2018-03-09 04:25:18

标签: android google-maps kotlin api-key

我在Android活动上尝试谷歌地图时遇到了一些问题。当我运行项目时,活动不显示任何内容,只显示空白活动。我尝试搜索解决方案,并尝试通过添加新的权限(如

)来修复它
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

尝试使用新密钥更改API密钥。但他们都失败了。我在stackoverflow中读到了一些解决方案,它说明了凭证中的包装名称(但是这个我不明白)。有人可以帮助我。

这是我的活动代码

package com.wahana.wahanamarketingclub.activities

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.google.android.gms.maps.*
import com.google.android.gms.maps.OnMapReadyCallback

import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.wahana.wahanamarketingclub.R

class EventActivity : AppCompatActivity(), OnMapReadyCallback {

private lateinit var mMap: GoogleMap

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_event)
    // Obtain the SupportMapFragment and get notified when the map is ready 
to be used.
    val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as MapFragment
    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 fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap

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

}

Mamnifest

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- <uses-permission android:name="android.permission.CAMERA" /> -->

<!--
     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" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>



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

    <!-- android:theme="@style/AppTheme"> -->
    <activity android:name=".activities.MainActivity" />
    <activity android:name=".activities.AboutUs" />
    <activity android:name=".activities.CatalogActivity" />
    <activity android:name=".activities.EventActivityNina" />
    <activity android:name=".activities.HistoryActivity" />
    <activity android:name=".activities.ActivitySalesmanActivity" />
    <activity android:name=".activities.QuizActivity" />
    <activity android:name=".activities.RewardActivity" />
    <activity android:name=".activities.TrainingScheduleActivity" />
    <activity android:name=".activities.ReportActivity" />
    <activity android:name=".activities.SurveyActivity" />
    <activity android:name=".activities.CustomerActivity" />
    <activity android:name=".activities.EditProfileActivity" />
    <activity android:name=".activities.ChangePasswordActivity" />
    <activity android:name=".activities.SplashScreenActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".activities.LoginActivity" />
    <!--
         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=".activities.EventActivity"
        android:label="@string/title_activity_event"></activity>
</application>

3 个答案:

答案 0 :(得分:1)

确保您已在配置console.developer.google.com中为项目添加了sha1密钥。有必要提供sha1键

答案 1 :(得分:0)

确保console.developer.google.com上的包裹名称正确且与您的包裹名称相同 com.wahana.wahanamarketingclub

另一个是确保您已启用 Google Maps Android API 。如果您启用了该功能,则会看到enter image description here

如果仍然无法正常工作,请在此处添加您的logcat以获取更多信息

答案 2 :(得分:0)

只要您在Android 9.0上进行操作,就会发现here存在问题,因此您发现此solution有用