SupportMapFragment does not support AndroidX Fragment
This issue has already been reported to Google in the public issue tracker:
https://issuetracker.google.com/issues/110573930
I would suggest starring the feature request in the public issue tracker to add your vote and subscribe to further notifications from Google. Hopefully, Google will implement it in next versions of Android Maps SDK.
Update
Google has provided the following answer in the public issue tracker
Hi all,
As noted the Google Maps API is currently based off the support libraries and not AndroidX. Future versions of Google Maps API will certainly support AndroidX, but in the meantime, the following workaround will resolve the issue for you:
Use Android Studio 3.2 (currently preview) or higher
Ensure your build.gradle contains 'compileSdkVersion 28' (default for new AS 3.2 projects)
Include the following in your gradle.properties file (default for new AS 3.2 projects)
android.useAndroidX=true
android.enableJetifier=true
Finally, for existing code referencing the support libraries, you can use the "Refactor -> Refactor to AndroidX" tool in Android Studio 3.2+ to automatically convert your code to use the new AndroidX packages.
AndroidX Fragment add Google Map
Finally I found the solution by using MapView (Which is commented in code) and calling onMapReady
by a little delay. Now map showing up fine with MapView.
override fun initUI() {
mapView?.postDelayed({
mapView?.onCreate(Bundle())
mapView?.getMapAsync {
onMapReady(it)
}
}, 500)
}
private fun onMapReady(googleMap: GoogleMap?) {
map = googleMap
// map?.setMinZoomPreference(14.0f)
val ny = LatLng(40.7143528, -74.0059731)
map?.addMarker(MarkerOptions().position(ny));
map?.moveCamera(CameraUpdateFactory.newLatLng(ny))
}
Cannot cast from Fragment to SupportMapFragment
Change;
googleMap = ((SupportMapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
with this;
googleMap = ((SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.map)).getMap();
Note that getSupportFragmentManager
is from android.support.v4.app.FragmentActivity.
Good luck.
Google maps SupportMapFragment error in Fragment
I solved that problem as this way.
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_partners_map, container, false);
} catch (InflateException e) {
}
First time I'm check that, is the Fragments parents view exists, then I recreate the parent, else I create it.
How to use google maps android SDK in a fragment?
You should be using <fragment>
tag not androidx.fragment.app.FragmentContainerView
. FragmentContainerView
is just a Container for fragments on which we can add fragments at runtime . on other hand <fragment>
is itself a fragment this why the android:name
attribute is there.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
If you using using it a fragment you should be using getChildFragmentManager()
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_view);
Update-> Yeah sorry i was wrong earlier . turns out FragmentContainerView
does support adding fragment in xml. Try the below code.
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/fragmentContainerView"
android:layout_width="match_parent"
android:tag="mapFragment"
android:layout_height="match_parent" />
Then you can get the map fragment by using #findFragmentByTag
.
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag("mapFragment");
Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment after migration of androidX
You have to use a SupportMapFragment
migrated from Android Support Libraries to AndroidX Libraries.
In you case use this version or later.
com.google.android.gms:play-services-maps:17.0.0
If you check the doc it extends androidx.fragment.app.Fragment
.
Related Topics
Android 4.3 and Phonegap, Cant Tap Link
Android: How to Implement First Time Tutorial Like Go Launcher in My App
Running Google Map Application on Android Emulator
Android Browser Ignores Responsive Web Design
Clearing Notification After a Few Seconds
How to Set The Font Style to Bold, Italic and Underlined in an Android Textview
Eclipse Hangs at The Android Sdk Content Loader
Pass 2D Array to Another Activity
How to View Data Saved in Android Database(Sqlite)
Android/View/View$Onunhandledkeyeventlistener (Onmeasure Error)
Android Device Configuration for Aosp
Android Listview Child View Setenabled() and Setclickable() Do Nothing
Android Fingerprint API for Time Attendance App
Find the Physical Address of Exception Vector Table from Kernel Module
How to Add Apache Commons Collections in Android Studio (Gradle)