android - 在获取 Google map fragment 时遇到问题调用 onMapReady

标签 android google-maps android-mapview

所以我遇到的问题是我不确定如何访问 map 来设置初始位置。 Fragment 总是返回 NULL,所以它似乎没有调用 getMapReady 函数。我只是不确定如何在布局中正确引用 map 小部件,以便它正确调用 map 的设置。

布局

<FrameLayout 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"
tools:context="com.example.user.thisweekintown.CalendarFragment">



    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/mySwipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/bus_list_recycle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="horizontal" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <fragment
        android:id="@+id/businessslocation"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="15dp"

        android:background="@color/cardview_light_background"/>
</FrameLayout>

代码

class BusListFragment : Fragment(),OnMapReadyCallback {
    lateinit var mMap: GoogleMap
    var mapView: SupportMapFragment? = null
    var totaladd:String? = null
.........

然后

 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        val view : View = inflater.inflate(R.layout.fragment_bus_list,container,false)
        var social_recycle: RecyclerView = view.findViewById(R.id.bus_list_recycle)

        Log.d("should be calling", "onMapReady")

        swipey = view.findViewById(R.id.mySwipe)
        swipey.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN, Color.CYAN);

        swipey.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener { setupRecycle(true) })
        calFrPrefs = context?.getSharedPreferences("main_prefs",0)

        (activity as MainActivity).getSupportActionBar()?.setTitle(calFrPrefs!!.getString("current_town_name","No Town")+ " Businesses")
        setupRecycle(false)
        return view

    }

然后

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mapView = childFragmentManager.findFragmentById(R.id.businessslocation) as? SupportMapFragment
        Log.d("isMapView", mapView.toString())

        mapView?.getMapAsync(this)

    }

它应该调用以下内容

override fun onMapReady(googleMap: GoogleMap){
        mMap = googleMap
        Log.d("onMapready", "MapReady")
        val cphbusiness = LatLng(42.187, 71.3065)
        mMap.addMarker(MarkerOptions().position(cphbusiness).title("Her"))
        mMap.moveCamera(CameraUpdateFactory.zoomTo(16.0F))
        mMap.moveCamera(CameraUpdateFactory.newLatLng(cphbusiness))

        val geocoder = Geocoder(context)
        if (geocoder is Geocoder) {
            var list: List<Address> = geocoder.getFromLocationName(totaladd, 1)
            if(list.size >0) {
                var mAddress: Address = list.get(0)
                Log.d("latitiude", mAddress.latitude.toString())
                Log.d("latitiude", mAddress.longitude.toString())
                var address1 = LatLng(mAddress.latitude, mAddress.longitude)
                changeMap(address1)
            }else
            {
                Toast.makeText(context, "Unable to find Valid Location...Centering on Random place", Toast.LENGTH_LONG).show()
                changeMap(cphbusiness)
            }
        } else {

        }

    }


    fun changeMap(myLL: LatLng) {
        mMap.moveCamera(CameraUpdateFactory.newLatLng(myLL))
        mMap.addMarker(MarkerOptions().position(myLL))

    }

最佳答案

最后,我通过将 fragment 名称从 android:name="com.google.android.gms.maps.MapFragment" 更改为 android:name="com.google. android.gms.maps.SupportMapFragment"

关于android - 在获取 Google map fragment 时遇到问题调用 onMapReady,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57750676/

相关文章:

android - Android 中更高效的 map 叠加

android - 谷歌地图 api v2 中标记顶部的标签

android - Android 中的 Material 形式

android - 我可以为 Firebase 通知设置自定义铃声和振动模式吗?

java - 为什么我的 Android 应用程序找不到 XML 文件中声明的 TextView 或按钮?

html - 三层透明

google-maps - 获取 MAP API 时 MD5 和 SHA1 有什么区别?

Android xxxhdpi 可绘制分辨率自动调整大小

javascript - 如何将地址转换为经纬度然后在页面上显示 map

android - 如何将指南针添加到 map View