我正在尝试使用MVP模式在AndroidX Fragment(androidx.fragment.app.Fragment)中添加Google地图,但未显示。但是,当我尝试在普通的Fragment(android.support.v4.app.Fragment)或Activity中使用此代码时,它显示的很好。
我尝试了两种方式:
使用MapView(在代码中注释了)
使用SupportMapFragment(AndroidX参考here不支持)
这是我尝试过的代码:
import android.os.Bundle
import com.google.android.gms.maps.*
import com.google.android.gms.maps.model.LatLng
import com.test.App
import com.test.R
import com.test.ui.base.BaseFragment
import javax.inject.Inject
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.SupportMapFragment
class OverviewMapFragment : BaseFragment<OverviewMapContract.View, OverviewMapContract.Presenter>(), OverviewMapContract.View, OnMapReadyCallback {
private var map: GoogleMap? = null
// private var mapView: MapView? = null
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
// mapView = view!!.findViewById(R.id.map_view) as MapView
// mapView!!.onCreate(savedInstanceState);
// mapView!!.getMapAsync(this)
}
/*override fun onStart() {
super.onStart()
mapView!!.onStart()
}
override fun onResume() {
super.onResume()
mapView!!.onResume()
}
override fun onDestroy() {
mapView!!.onDestroy()
super.onDestroy()
}
override fun onLowMemory() {
super.onLowMemory()
mapView!!.onLowMemory()
}
override fun onStop() {
super.onStop()
mapView!!.onStop()
}
override fun onPause() {
mapView!!.onPause()
super.onPause()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView!!.onSaveInstanceState(outState)
}*/
override fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
map!!.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map!!.addMarker(MarkerOptions().position(ny));
map!!.moveCamera(CameraUpdateFactory.newLatLng(ny))
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
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"
tools:context="com.test.ui.components.overviewmap.OverviewMapFragment" />
</LinearLayout>
清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
<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_key" />
<uses-library android:name="com.google.android.maps" />
使用的googleMapVersion 16.0.0:
implementation 'com.google.android.gms:play-services-maps:$googleMapVersion'
答案 0 :(得分:0)
最后,我找到了解决方案,方法是使用MapView(在代码中有注释)并稍加延迟地调用onMapReady
。现在,使用MapView可以很好地显示地图。
override fun initUI() {
mapView?.postDelayed({
mapView?.onCreate(Bundle())
mapView?.getMapAsync {
onMapReady(it)
}
}, 500)
}
private fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
// map?.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map?.addMarker(MarkerOptions().position(ny));
map?.moveCamera(CameraUpdateFactory.newLatLng(ny))
}