Followed a tutorial to work with Google Maps and it works well. Now I'd like to have this map placed within a fragment for a TableLayout
and can't figure out how to change the code for SupportFragmentManager
(which drops an error). I get "UNREACHABLE CODE" when I replace it with childFragmentManager
.
Here is my code:
class FragItLocate : Fragment(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var mMap: GoogleMap
override fun onMarkerClick(p0: Marker?)=false
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.frag_it_locate,container,false)
val mapFragment = childFragmentManager
.findFragmentById(R.id.map2) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mMap.setOnMarkerClickListener(this)
mMap.uiSettings.isZoomControlsEnabled = true
// Add a marker in Atlantic Lawn and Garden and move the camera
val alg = LatLng(41.4958611, -71.3768593)
mMap.addMarker(MarkerOptions().position(alg).title("Atlantic Lawn and Garden"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(alg))
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(alg, 17.05f))
}
}
Update: I've tried the answer but my app crash with this error
Because when you call this line
return inflater.inflate(R.layout.frag_it_locate, container, false)
it will exit onCreateView
method and do not execute all statements after that line. That why your get UNREACHABLE CODE
error.
So change your code to
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val mapFragment = childFragmentManager
.findFragmentById(R.id.map2) as SupportMapFragment
mapFragment.getMapAsync(this)
return inflater.inflate(R.layout.frag_it_locate, container, false);
}
Update: You got that error because this line.
val mapFragment = childFragmentManager
.findFragmentById(R.id.map2) as SupportMapFragment
If findFragmentById(R.id.map2)
return null
, your app will crash because you are using Unsafe Cast
operator.
You can prevent that by using Smart Cast
operator.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val mapFragment = childFragmentManager
.findFragmentById(R.id.map2) as SupportMapFragment?
mapFragment?.getMapAsync(this)
return inflater.inflate(R.layout.frag_it_locate, container, false);
}
Update 2: Based on your comment about onMapReady
not called.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val mapFragment = childFragmentManager
.findFragmentById(R.id.map2) as SupportMapFragment?
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
childFragmentManager.beginTransaction().replace(R.id.map2, mapFragment).commit();
}
mapFragment?.getMapAsync(this)
return inflater.inflate(R.layout.frag_it_locate, container, false);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments