0

我正在使用HereMaps在 ViewPager2中的子片段中显示地图。但是当地图被初始化时,应用程序会暂时冻结,直到地图被初始化或失败。地图初始化完成后,我就可以触摸 UI。

我有一个Activity, 构建页面适配器。第一个片段有一个用于地图视图的子片段。

我究竟做错了什么?即使地图未完成初始化,我也希望能够向下滚动当前片段或导航离开该片段或活动。

活动布局

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <TextView
            android:id="@+id/call_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minHeight="?actionBarSize"
            android:padding="@dimen/appbar_padding"
            android:text="@string/app_name"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorAccent" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/sectionViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

加载的视图寻呼机的第一个片段

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/frame_padding">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

       
        <include /// blah other fragments />

        <FrameLayout
            android:id="@+id/mapFragmentContainer"
            android:layout_width="0dp"
            android:layout_height="@dimen/widget_map_size_height"
            android:layout_marginTop="@dimen/section_margin_vertical_20"
            android:background="@drawable/background_rounded_corners"
            android:elevation="1dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/some_other_section_id" />

     <include /// blah other fragments />

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

嵌入在第一个片段中的地图片段

<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/mapFragment"
        class="com.here.android.mpa.mapping.AndroidXMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="heremaps"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/progressBarMaps"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

第一个片段类

class MyFirstFragment : Fragment(R.layout.my_first_fragment) {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        activity?.let {
            // Add map preview
            childFragmentManager.beginTransaction().replace(R.id.mapFragmentContainer, MapViewFragment()).commit()

        }
    }
}

MapViewFragment类_

class MapViewFragment : Fragment(R.layout.component_mapview) {
    private val TAG = "MapViewFragment"

    private var m_map: Map? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initMapFragmentView()
    }
    private fun initMapFragmentView() {
        
        val success = com.here.android.mpa.common.MapSettings.setIsolatedDiskCacheRootPath(requireContext().applicationContext.filesDir.absolutePath  + "/.isolated-here-maps")

        if (!success) {
            Log.e(TAG, "FAILURE DISK CACHE")
            return
        }

        val mapFragment = childFragmentManager.findFragmentById(R.id.mapFragment) as AndroidXMapFragment?

        val context = ApplicationContext(requireContext()).apply {
            setAppIdCode(requireContext().getString(R.string.heremaps_appid), requireContext().getString(R.string.heremaps_appcode))
            setLicenseKey(requireContext().getString(R.string.heremaps_licensekey))
        }

        Log.i(TAG, "Starting to build map frag")

        mapFragment?.let { fragment ->

            fragment.init(context) { error ->

                when (error) {
                    OnEngineInitListener.Error.NONE -> {
                        progressBarMaps?.visibility = View.GONE

                        m_map = fragment.map

                        m_map?.run {
                            setCenter(GeoCoordinate(52.500556, 13.398889, 0.0), Map.Animation.NONE)
                            setZoomLevel((maxZoomLevel + minZoomLevel) / 2);
                        }
                        Toast.makeText(requireContext(), "Map Engine initialized successfully!", Toast.LENGTH_LONG).show();
                    }
                    else -> {
                        val errorMessage = "Error: ${error}, SDK Version: ${Version.getSdkVersion()}"
                        Log.i(TAG, "ERROR: $errorMessage")
                        progressBarMaps?.visibility = View.GONE
                        Toast.makeText(requireContext(), errorMessage, Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
    }
}
4

1 回答 1

0

这里设置了条件语句,它指出除非地图没有初始化,否则不会启用其他 UI 功能,因此除非地图没有完全渲染,否则需要一段时间。您可以尝试删除 init 条件语句。

mapFragment?.let { fragment ->

   fragment.init(context) { error ->

当应用程序冻结或崩溃时,您可以帮助处理任何其他日志吗?

于 2020-07-14T09:58:16.450 回答