我只看到带有谷歌标志的灰色、空的、空白的窗口
-
活动碎片_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>
在谷歌地图活动项目/应用程序地图创建的密钥是正常工作的
您必须在AndroidManifest.xml
中添加API密钥以启用必要的API
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="PASTE-YOUR-API-KEY-HERE" />
检查此项以获取API密钥
在AndroidManifest.xml 中删除
<dist:module dist:instant="true" />