AndroidX Fragment 添加谷歌地图

标签 android android-fragments google-maps-android-api-2 androidx

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

两种方法我都试过了:

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

  2. 使用 SupportMapFragment(AndroidX ref 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>

list :

<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 可以很好地显示 map 。

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))
    }

关于AndroidX Fragment 添加谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53407044/

相关文章:

android - 为整个布局设置动画

Android Google Maps v2 - 将对象添加到标记

android-layout - 影响工具栏样式和定位的抽屉导航片段

android-fragments - 片段中的 GoogleApiClient.Builder.enableAutoManage 抛出 IllegalStateException : Recursive entry to executePendingTransactions

android - 尝试在空对象引用上调用虚拟方法 'void android.graphics.Bitmap.copyPixelsFromBuffer(java.nio.Buffer)'

android - 隐藏在透明拆分操作栏后面的 Google 标题 Google Maps API v2

android - 膨胀谷歌地图 v2 fragment 导致 ClassNotFoundException

Android:从 native 数据源填充 ListView 的最佳方式

Android ListView,每个列表项有一个复选框和多个 TextView

java - 使用 Google Maps API v2 获取行车路线