Mapbox 无法显示设备位置

Mapbox is unable to show device location

我有一个带有 Mapbox 的片段,我想在上面显示设备位置。

class SampleMapFragment : Fragment(), PermissionsListener {

    private lateinit var binding: FragmentExploreBinding

    @Inject
    lateinit var permissionsManager: PermissionsManager

    private lateinit var mapboxMap: MapboxMap

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Mapbox.getInstance(requireContext().applicationContext, getString(R.string.mapbox_token))
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (!::binding.isInitialized) {
            binding = DataBindingUtil.inflate(
                inflater,
                R.layout.fragment_explore,
                container,
                false
            )

            binding.lifecycleOwner = this

            binding.mapView.onCreate(savedInstanceState)
            setUpMap()
        }

        return binding.root
    }

    private fun setUpMap() {
        binding.mapView.getMapAsync { mapboxMap ->
            this.mapboxMap = mapboxMap
            mapboxMap.setStyle(Style.MAPBOX_STREETS) { loadedMapStyle ->
                starLocationTracking(loadedMapStyle)
            }
        }
    }

    private fun starLocationTracking(loadedMapStyle: Style) {
        if (!PermissionsManager.areLocationPermissionsGranted(requireContext())) {
            permissionsManager.requestLocationPermissions(requireActivity())
            return
        }

        initLocationComponent(loadedMapStyle)
    }

    private fun initLocationComponent(loadedMapStyle: Style) {

        val customLocationComponentOptions = LocationComponentOptions.builder(requireActivity())
            .elevation(5f)
            .accuracyAlpha(.6f)
            .accuracyColor(Color.RED)
            .build()

        val locationComponentActivationOptions = LocationComponentActivationOptions.builder(
            requireActivity(),
            loadedMapStyle
        )
            .locationComponentOptions(customLocationComponentOptions)
            .build()

        mapboxMap.locationComponent.apply {
            activateLocationComponent(locationComponentActivationOptions)
            isLocationComponentEnabled = true
            renderMode = RenderMode.COMPASS
            cameraMode = CameraMode.TRACKING
        }
    }

    override fun onExplanationNeeded(permissionsToExplain: MutableList<String>?) {
        // TODO some explanation can be shown here
    }

    override fun onPermissionResult(granted: Boolean) {
        if (granted) mapboxMap.getStyle { loadedStyle -> starLocationTracking(loadedStyle) }
        //else TODO some explanation can be shown here
    }

    fun onMapBoxRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) = permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults)

    override fun onStart() {
        super.onStart()
        binding.mapView.onStart()
    }

    override fun onResume() {
        super.onResume()
        binding.mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        binding.mapView.onPause()
    }

    override fun onStop() {
        super.onStop()
        binding.mapView.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        releaseMapResources()
        releasePermissionsManagerListener()
    }

    private fun releaseMapResources() {
        binding.mapView.onDestroy()
    }

    private fun releasePermissionsManagerListener() {
        permissionsManager.listener = null
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.mapView.onSaveInstanceState(outState)
    }

    override fun onLowMemory() {
        super.onLowMemory()
        binding.mapView.onLowMemory()
    }
}

我用 2 个真实设备和 1 个模拟器测试了上面的实现。 该解决方案适用于 Android 10 设备。找到用户位置,即使关闭定位服务并在地图后再次打开它,也是可见的。

但有时找不到位置,有时需要很长时间,在尝试使用 Android 9 和 Android 7 设备时,甚至在地图可见之前位置服务就已经准备就绪。

这可能是什么问题?任何帮助将不胜感激。

"But location is sometimes not found, sometimes it take very long, even the location services are ready before the map is visible while trying with Android 9 and Android 7 devices."

这听起来好像设备试图从 GPS 传感器获取位置,但需要很长时间才能检索到。使用较新的设备和 Android 10 本地化已得到改进,因此这可以解释您所看到的内容。

要了解发生了什么,我建议实施 Android FusedLocationProvider 并将行为与 Mapbox SDK (locationComponent) 包装的行为进行比较。

此外,请检查您使用 Android 10 获得的 "snappy" 位置是否实际上是从 GPS 传感器检索到的,或者如果它是 "just" 最后已知的位置,那是由 Android.

缓存