繁体   English   中英

浮动操作按钮在 Mapbox 上不可见 Android

[英]Floating Action Button not visible over Mapbox Android

自从我开发 Android 应用程序以来已经有一段时间了。 所以目前的开发工作对我来说是很新的。 我正在尝试创建一个基于 Mapbox Android API 的应用程序。 到目前为止,我已成功加载用户位置并根据位置变化移动相机; 但是,我在布局上添加的 FAB 没有显示在模拟器或我的手机中。 它在 XML 设计视图中可见。 我相信这可能是由于 MapsActivity.kt 中的setContentView(mapView) ,我不知道它是如何无法加载整个布局的。 我错过了什么吗?

activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <com.mapbox.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        mapbox:mapbox_cameraTargetLat="40.7128"
        mapbox:mapbox_cameraTargetLng="-74.0060"
        mapbox:mapbox_cameraZoom="9.0">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:backgroundTint="@color/purple_200"
        android:layout_margin="16dp"
        android:contentDescription="@string/fab_description"
        android:src="@drawable/ic_plus_icon"
        tools:ignore="ImageContrastCheck" />
    </com.mapbox.maps.MapView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

MapsActivity.kt

package com.example.pinpoint

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.ActivityCompat
import com.example.pinpoint.com.example.pinpoint.LocationInitHelper

import com.example.pinpoint.com.example.pinpoint.LocationListeningCallback
import com.example.pinpoint.databinding.ActivityMapsBinding

import com.mapbox.android.core.location.LocationEngine
import com.mapbox.android.core.location.LocationEngineProvider
import com.mapbox.android.core.location.LocationEngineRequest
import com.mapbox.maps.CameraOptions
import com.mapbox.maps.MapView
import com.mapbox.maps.Style
import com.mapbox.maps.plugin.gestures.gestures
import com.mapbox.maps.plugin.locationcomponent.location

import java.lang.ref.WeakReference


class MapsActivity : AppCompatActivity() {

    private lateinit var mapView: MapView
    private lateinit var locationEngine: LocationEngine

    private val callback = LocationListeningCallback(this)
    private lateinit var locationPermissionHelper: LocationPermissionHelper
    private lateinit var locationInitHelper: LocationInitHelper

    private lateinit var binding: ActivityMapsBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMapsBinding.inflate(layoutInflater)
        mapView = MapView(binding.root.context)
        setContentView(mapView)
        locationInitHelper = LocationInitHelper(WeakReference(this))
        locationInitHelper.setMapView(mapView)
        mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS)
        locationEngine = LocationEngineProvider.getBestLocationEngine(this)
        locationPermissionHelper = LocationPermissionHelper(WeakReference(this))
        onMapReady()
    }

    private fun onMapReady(){

        val DEFAULT_INTERVAL_IN_MILLISECONDS = 1000L
        val DEFAULT_MAX_WAIT_TIME = DEFAULT_INTERVAL_IN_MILLISECONDS * 5

        locationEngine = LocationEngineProvider.getBestLocationEngine(this)
        var request = LocationEngineRequest.Builder(DEFAULT_INTERVAL_IN_MILLISECONDS)
            .setPriority(LocationEngineRequest.PRIORITY_NO_POWER)
            .setMaxWaitTime(DEFAULT_MAX_WAIT_TIME)
            .build()

        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            locationPermissionHelper.checkPermissions { onMapReady() }
        }
        locationEngine.requestLocationUpdates(request, callback, mainLooper)
        locationEngine.getLastLocation(callback)
        mapView.getMapboxMap().setCamera(
            CameraOptions.Builder()
                .zoom(12.0)
                .build()
        )
        locationInitHelper.initLocationComponent()
        locationInitHelper.setupGesturesListener()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView.location
            .removeOnIndicatorBearingChangedListener(locationInitHelper.onIndicatorBearingChangedListener)
        mapView.location
            .removeOnIndicatorPositionChangedListener(locationInitHelper.onIndicatorPositionChangedListener)
        mapView.gestures.removeOnMoveListener(locationInitHelper.onMoveListener)
    }

    override fun onStop() {
        super.onStop()
        locationEngine.removeLocationUpdates(callback)
    }

}

尝试这个

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.mapbox.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        mapbox:mapbox_cameraTargetLat="40.7128"
        mapbox:mapbox_cameraTargetLng="-74.0060"
        mapbox:mapbox_cameraZoom="9.0">
    </com.mapbox.maps.MapView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:backgroundTint="@color/purple_200"
        android:layout_margin="16dp"
        android:src="@drawable/ic_app_icon"
        android:tint="@color/white"
        tools:ignore="ImageContrastCheck" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

尝试像这样在 mapview 之外声明浮动操作按钮:

    <?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <com.mapbox.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        mapbox:mapbox_cameraTargetLat="40.7128"
        mapbox:mapbox_cameraTargetLng="-74.0060"
        mapbox:mapbox_cameraZoom="9.0"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:backgroundTint="@color/purple_200"
        android:layout_margin="16dp"
        android:contentDescription="@string/fab_description"
        android:src="@drawable/ic_plus_icon"
        tools:ignore="ImageContrastCheck" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我能够通过 MapsActivity.kt 解决这个问题。 因为我怀疑setContentView()得到了错误的视图,所以我替换了那个部分并手动将视图分配给mapView 该脚本如下所示:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_maps)

        mapView = findViewById(R.id.mapView)

        locationInitHelper = LocationInitHelper(WeakReference(this))
        locationInitHelper.setMapView(mapView)

        mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS)

        locationEngine = LocationEngineProvider.getBestLocationEngine(this)

        locationPermissionHelper = LocationPermissionHelper(WeakReference(this))

        onMapReady()
    }

感谢所有的答案!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM