Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

433
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!