Google地图未显示在底部导航活动片段中(kotlin)

我只能看到带有Google徽标的灰色,空白空白窗口

  1. 活动fragment_map

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  2. MapFragment类

    class MapFragment : Fragment(),OnmapReadyCallback{
    
    private lateinit var mMap: GoogleMap
    

在onCreateView

    // Inflate the layout for this fragment
    var rootView = inflater.inflate(R.layout.fragment_map,container,false)

    var mapFragment = getchildFragmentManager().findFragmentById(R.id.map) as SupportMapFragment
    mapFragment?.getMapAsync(this)

    return rootView

更多代码

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))
}
  1. 已启用SDK,已添加API密钥并在Google API中配置了SHA1

  2. AndroidManifest

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:dist="http://schemas.android.com/apk/distribution"
        package="com.domain.name">
    
        <uses-permission android:name="android.permission.accESS_FINE_LOCATION" />
    
        <dist:module dist:instant="true" />
    
        <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">
    
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
    
            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="@string/google_maps_api_key"/>
    
            <activity
                android:name=".Mainactivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    
  3. 在Strings.xml中

        <string name="google_maps_api_key">AIz....2Y</string>
    

在Google Map activity项目/应用程序地图中创建的密钥正常工作

lakewent20091231 回答:Google地图未显示在底部导航活动片段中(kotlin)

您必须在AndroidManifest.xml中添加 API密钥才能启用必要的API

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="PASTE-YOUR-API-KEY-HERE" />

Check this获取 API密钥

,

在AndroidManifest.xml中删除

android.util.Base64
本文链接:https://www.f2er.com/3154643.html

大家都在问