Home > OS >  map view marker not always visible
map view marker not always visible

Time:10-14

firstly, the code:

class MapFragment: BaseFragment<MapFragmentBinding>(R.layout.map_fragment) {
    var mapView: MapView? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mapView = binding.mapView
        MapsInitializer.initialize(requireContext(),
            MapsInitializer.Renderer.LATEST
        ) {}
        // seems a bit out of place, but due to the binding variable, from out baseFragment class, it has to be done here
        mapView?.onCreate(savedInstanceState)
        mapView?.getMapAsync { gMap ->
            gMap.setOnMapLoadedCallback {
                val randomLocation = LatLng(
                    Random.nextDouble(-170.0, 170.0),
                    Random.nextDouble(-170.0, 170.0)
                )
                gMap.addMarker(MarkerOptions().position(randomLocation).title(randomLocation.toString()))
                gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(randomLocation, 10.0f))
            }
        }
    }

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

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

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

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

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

    override fun onDestroy() {
        super.onDestroy()
        mapView?.onDestroy()
    }

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

this is a mapFragment that is included into another fragment. It is reloaded fairly often (to be precise, the app has a recycler view filled with items, whenever one is pressed this fragment is visible).

I am not sure why but sometimes (whenever it is opened and closed 3-7 times) it animates the camera to random location, and does not display the marker at all. I noticed that it happens mostly whenever the "random location" is at Arctic Ocean or Antarctica (believe me I don't know whether that is related at all, I am as confused as you are).

Any hints/ideas why that might be happening?

CodePudding user response:

Your range of a random latitude is invalid -170.0, 170.0. The map camera implementation will likely peg the out-of-range value to -90 or 90 and the map will refuse to place the marker.

A maximum range for random position could use :

val randomLocation = LatLng(
                Random.nextDouble(-90.0, 90.0),  // kotlin Random produces [-90,90)
                Random.nextDouble(-180.0, 180.0)

The maps api for latitude permits:

[-90, 90]    // inclusive at both ends

and for longitude permits:

[-180, 180)  // inclusive on the negative and exclusive on the positive
  • Related