Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

435
Visualizações
Map Fragment(Kotlin) onMapReady not working

I'm trying to implement a map in a fragment and I am able to show the map but the onMapReady is not working as Its not adding the marker and not moving the camera. Code

MapFragment

class MapFragment : Fragment(), OnMapReadyCallback {

   private lateinit var mMap: GoogleMap

    companion object {
        var mapFragment : SupportMapFragment?=null
        val TAG: String = MapFragment::class.java.simpleName
        fun newInstance() = MapFragment()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        var rootView = inflater.inflate(R.layout.fragment_map, container, false)

            mapFragment = fragmentManager?.findFragmentById(R.id.fallasMap) as SupportMapFragment?
            mapFragment?.getMapAsync(this)

        return rootView
    }

    override fun onMapReady(googleMap: GoogleMap?) {
        mMap = googleMap!!
        // Add a marker in Sydney and move the camera
        val sydney = LatLng(-34.0, 151.0)
        mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }

}

xml

<?xml version="1.0" encoding="utf-8"?>
<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=".fragments.MapFragment">

        <fragment
                android:id="@+id/fallasMap"
            android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

</FrameLayout>
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Your using the wrong fragment manager to access it. Since it's a fragment within a fragment, it's added to the childFragmentManager.

childFragmentManager.findFragmentById(R.id.fallasMap) as SupportMapFragment

Also use the as? cast operator instead of casting to the optional type. Or even do a normal cast, since you need the fragment to be correct and null would be a bug.

Also don't store a static reference to the fragment if you don't clean it up properly.

over 4 years ago · Santiago Trujillo Relatório

0

You shoud pass Value googleMap to mMap to InterAct with your markers

        override fun onMapReady(googleMap: GoogleMap?) {
        mMap = googleMap 
       // Add a marker in Sydney and move the camera
        val sydney = LatLng(-34.0, 151.0)
        mMap.addMarker(MarkerOptions().position(sydney).title("Marker in  
        Sydney"))
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
      }
over 4 years ago · Santiago Trujillo Relatório

0

It's not correct to share these variables globale in your case, You should remove them from the companion object and create a new instance each time you open fragment.

When the rootView != null the onMapReady method won't called.

So I suggesting to remove them from companion object and put them in onCreateView without the condition if (rootView == null)(remove the condition).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda