我只能看到带有Google徽标的灰色,空白空白窗口
活动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>
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))
}
已启用SDK,已添加API密钥并在Google API中配置了SHA1
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>
在Strings.xml中
<string name="google_maps_api_key">AIz....2Y</string>
在Google Map Activity项目/应用程序地图中创建的密钥正常工作
答案 0 :(得分:0)
您必须在AndroidManifest.xml
中添加 API密钥才能启用必要的API
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="PASTE-YOUR-API-KEY-HERE" />
Check this获取 API密钥
答案 1 :(得分:0)
在AndroidManifest.xml中删除
android.util.Base64