Android 谷歌地图不会出现,只有谷歌徽标会出现在emulator中

Android 谷歌地图不会出现,只有谷歌徽标会出现在emulator中,android,google-maps,kotlin,Android,Google Maps,Kotlin,我在加载地图活动时遇到问题。当我尝试运行应用程序时,在地图活动中,只有谷歌徽标出现在底部,但没有地图标志,如下图所示。 我不知道为什么会发生这种情况,因为我启用了gps并连接到了互联网,还使用了map api密钥 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.a

我在加载地图活动时遇到问题。当我尝试运行应用程序时,在地图活动中,只有谷歌徽标出现在底部,但没有地图标志,如下图所示。 我不知道为什么会发生这种情况,因为我启用了gps并连接到了互联网,还使用了map api密钥

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

    <!--
         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="android.permission.INTERNET"/>
    <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">
        <activity android:name=".Controller.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Controller.selection" />
        <!--
             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="AIzaSyBJJpVVf6DZcQrl-*************" />

        <activity
            android:name=".Controller.MapsActivity"
            android:label="@string/title_activity_maps"></activity>
    </application>

</manifest>

现在这是活动代码

package com.example.abdus.touristswatch.Controller

import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import com.example.abdus.touristswatch.R

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
import java.util.jar.Manifest

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
     var locationManager:LocationManager?=null
    var locationListener:LocationListener?=null
    private lateinit var mMap: GoogleMap

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_maps)
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
                .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if(grantResults.size>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED ){

            if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
                locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0f,locationListener)
                var location:Location=locationManager!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
                updateMap(location)

               }


        }


    }

    fun updateMap(location:Location){
        val userLocation = LatLng(location.getLatitude(), location.getLongitude())
         mMap.clear()
        mMap.addMarker(MarkerOptions().position(userLocation).title("Your Location"))
        mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation))
    }
    /**
     * 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
         locationManager=getApplicationContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager
         locationListener=(object:LocationListener{
             override fun onLocationChanged(location: Location?) {
                 updateMap(location!!)
             }

             override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {

             }

             override fun onProviderEnabled(provider: String?) {

             }

             override fun onProviderDisabled(provider: String?) {

             }

         })
          if(Build.VERSION.SDK_INT<23){
              if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
              locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0f,locationListener)}
          }
          else{
              if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
                 var permissionlist=arrayOf<String>(android.Manifest.permission.ACCESS_FINE_LOCATION)
                  ActivityCompat.requestPermissions(this,permissionlist,1)
              }
              else{
                  locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0f,locationListener)
                  var location:Location=locationManager!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
                  updateMap(location)

              }
          }

        // Add a marker in Sydney and move the camera

    }
}
package com.example.abdus.touristswatch.Controller
导入android.content.Context
导入android.content.pm.PackageManager
导入android.location.location
导入android.location.LocationListener
导入android.location.LocationManager
导入android.os.Build
导入android.support.v7.app.AppActivity
导入android.os.Bundle
导入android.support.v4.app.ActivityCompat
导入android.support.v4.content.ContextCompat
导入com.example.abdus.touristswatch.R
导入com.google.android.gms.maps.CameraUpdateFactory
导入com.google.android.gms.maps.GoogleMap
导入com.google.android.gms.maps.OnMapReadyCallback
导入com.google.android.gms.maps.SupportMapFragment
导入com.google.android.gms.maps.model.LatLng
导入com.google.android.gms.maps.model.MarkerOptions
导入java.util.jar.Manifest
类MapsActivity:AppCompatActivity(),OnMapReadyCallback{
var locationManager:locationManager?=null
var locationListener:locationListener?=null
私有lateinit var mMap:GoogleMap
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u映射)
//获取SupportMapFragment,并在地图准备好使用时收到通知。
val mapFragment=supportFragmentManager
.findFragmentById(R.id.map)作为SupportMapFragment
getMapAsync(此)
}
重写onRequestPermissionsResult(请求代码:Int,权限:Array,GrantResult:IntArray){
super.onRequestPermissionsResult(请求代码、权限、GrantResult)
if(grantResults.size>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u FINE\u LOCATION)==PackageManager.permission\u已授予){
locationManager!!.RequestLocationUpdate(locationManager.GPS\u提供程序,0,0f,locationListener)
变量位置:location=locationManager!!.getLastKnownLocation(locationManager.GPS\U提供程序)
更新映射(位置)
}
}
}
乐趣更新映射(位置:位置){
val userLocation=LatLng(location.getLatitude(),location.getLongitude())
mMap.clear()
mMap.addMarker(MarkerOptions().position(userLocation).title(“您的位置”))
mMap.moveCamera(CameraUpdateFactory.newLatLng(用户位置))
}
/**
*一旦可用,就可以操纵贴图。
*当映射准备好使用时,将触发此回调。
*这是我们可以添加标记或线条、添加侦听器或移动摄影机的地方。在这种情况下,
*我们只是在澳大利亚悉尼附近加了一个标记。
*如果设备上未安装Google Play服务,系统将提示用户安装
*它位于SupportMapFragment内。此方法仅在用户
*已安装Google Play服务并返回应用程序。
*/
在mapready上覆盖乐趣(谷歌地图:谷歌地图){
mMap=谷歌地图
locationManager=getApplicationContext().getSystemService(Context.LOCATION\u服务)作为locationManager
locationListener=(对象:locationListener{
覆盖乐趣onLocationChanged(位置:位置?){
更新映射(位置!!)
}
重写fun onStatusChanged(提供程序:String?,状态:Int,附加:Bundle?){
}
重写onProviderEnabled(提供程序:字符串?){
}
override fun onProviderDisabled(提供程序:字符串?){
}
})

如果(Build.VERSION.SDK_int)您不应该发布您的API_密钥在真实设备上有效吗?您能在LogCat中看到任何错误消息吗?您是否在app/Build.gradle文件中添加了谷歌地图和位置依赖项?是的,我添加了谷歌地图和位置依赖项