我只能看到带有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项目/应用程序地图中创建的密钥正常工作