Reputation: 3545
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>
Upvotes: 4
Views: 9062
Reputation: 31
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mapFragment = activity?.supportFragmentManager?.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
}
Upvotes: 0
Reputation: 39843
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.
Upvotes: 14
Reputation: 318
Use
supportFragmentManager
to findSupportMapFragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
var rootView = inflater.inflate(R.layout.fragment_map, container, false)
mapFragment = supportFragmentManager?.findFragmentById(R.id.fallasMap) as SupportMapFragment?
mapFragment?.getMapAsync(this)
return rootView
}
If you're not customizing you fragment then inside your XML file too
<fragment
android:id="@+id/fallasMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Upvotes: 1
Reputation:
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).
Upvotes: 2
Reputation: 201
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))
}
Upvotes: 3