AndroidX Fragment 添加 Google 地图

AndroidX Fragment add Google Map

我正在尝试使用 MVP 模式在 AndroidX Fragment(androidx.fragment.app.Fragment) 中添加一个 Google 映射,但它没有显示出来。但是当我尝试在正常 Fragment(android.support.v4.app.Fragment) 或 Activity 中使用此代码时,它显示正常。

两种方法我都试过了:

  1. 使用 MapView(在代码中注释)

  2. 使用 SupportMapFragment(AndroidX ref 不支持)

这是我试过的代码:

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' 

最后我通过使用 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))
    }