How do I know the map is ready to get used when using the SupportMapFragment?
EDIT: getMap
is deprecated now
The problem is when the call to getMap is null, when can I try again?
That depends upon the nature of the problem.
If you set up the SupportMapFragment
via the <fragment>
element in the layout, you can call getMap()
successfully in onCreate()
. But, if you create the SupportMapFragment
via the constructor, that's too soon -- the GoogleMap
does not yet exist. You can extend SupportMapFragment
and override onActivityCreated()
, as getMap()
is ready by then.
However, getMap()
can also return null
for a bigger problem, such as Google Play Services not being installed. You would need to use something like GooglePlayServicesUtil.isGooglePlayServicesAvailable()
to detect this condition and deal with it however you wish.
Android - How to know if the Google Map is ready?
After looking into the other answers I made a much simpler option myself:
mMapFragment = new SupportMapFragment() {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
setupMapIfNeeded();
return view;
}
};
This way, I extend the SupportMapFragment
anonymously, and I don't need to create a new class or interfaces or listeners. Although if you need to reuse things and can benefit for more complex use cases, I recommend you to extend it in a new class and setup some listnets, but for my purpouse this was more than necessary.
Word of advice: Using it this way you can't use the factory method newInstance()
of the SupportMapFragmet
, so you will have to use the constructor, which for me is working just fine, but some advice to not use it.
Is there a way to get notified when the GoogleMap is ready in a MapView object in the new Google Maps Android API v2?
To use the MapView directly you need to make sure you forwarding the Activity lifecycle methods through to the MapView.
E.g. to set it up, you'd do this in your onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.raw_mapview_demo);
mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((MapView) findViewById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
The code is taken from Google's sample code that is included with the Play Services lib. You also need to route onDestroy(), onResume(), and onPause().
FYI you need to check for a null pointer for both the MapView and MapFragment as the user may not have Google Play Services installed. If it isn't installed the user should get a prompt to install the APK and then you can return to this Activity.
Hope that helps.
Ryan
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 SupportMapFragment inside a Fragment?
I just met my luck, while making this post,I found what Im looking for.
I have used this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_location, container, false);
mMapFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mMap = mMapFragment.getMap();
if (mMap != null) {
setupMap();
}
}
};
getChildFragmentManager().beginTransaction().add(R.id.framelayout_location_container, mMapFragment).commit();
return v;
}
Credit to this Old post
How can i get a reference of a Fragment (SupportMapFragment) loaded by the NavHostFragment (of the Navigation Architecture components)
Instead of adding the SupportMapFragment
directly to your navigation graph, you should add your own Fragment class that will own and manage the SupportMapFragment
- it would be the one to call getMapAsync
and have the SupportMapFragment
in its own layout.
The goal of Navigation is to decouple the Activity from the contents of the individual destinations. Having the Activity manage a single destination is not a recommended pattern.
Not able to show Google Map inside the fragment
Add
android:name="com.google.android.gms.maps.SupportMapFragment"
in your xml
like
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map_fragment"/>
Edit
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
For more Information visit https://developers.google.com/maps/documentation/android-sdk/start#step_5_hello_map_take_a_look_at_the_code
Related Topics
Android - Out of Memory Exception When Creating Bitmap
Android Studio - Unable to Merge from Svn Branch
Org.JSON.JSON Exception:End of Input at Character 0
Marking Sms Messages as Read/Unread or Deleting Messages Not Working in Kitkat
Unresolved Reference - Activity Does Not Recognize Synthetic Imports in Android Studio V4
How to Put Gridview Inside Scrollview
Have Both Gms and Hms in the Project
Google Play Game Services - Unable to Sign In
Wifi Connect-Disconnect Listener
Android M Permissions:Confused on the Usage of Shouldshowrequestpermissionrationale() Function
Fade in Fade Out Android Animation in Java
Was Preferencefragment Intentionally Excluded from the Compatibility Package
Android Adb Device Offline, Can't Issue Commands
Navigation Drawer: How to Set the Selected Item at Startup
Textwatcher for More Than One Edittext