I'm facing problems when placing SupportMapFragment inside a fragment that is part of a viewpager with 5 tabs, everything works correctly when viewpager loads the map for first time, but not showing anything whe comming back from another tab. I load the map as follows:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_estate);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_estate, mapFragment).commit();
}
mapFragment.getMapAsync(this);
}
and then use it like:
#Override
public void onMapReady(GoogleMap googleMap) {
this.map = googleMap;
this.map.setMyLocationEnabled(true);
this.map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
this.map.getUiSettings().setMyLocationButtonEnabled(false);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(center, 16);
mapFragment.getMap().moveCamera(cameraUpdate);
}
Anyone facing same problem? Thanks in advance
ReplacingFragmentManager fm = getFragmentManager(); for FragmentManager fm = getChildFragmentManager(); solved the problem.
Related
I am trying to use Google Map in an Android project. The details of a person will be displayed in row along with a button on clicking which the location will be shown using Latitude and longitude saved against that person. I am using fragment for display purpose.
activity_show_map.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
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"
tools:context="com.example.dioglgt.myapplication.ClinicFragment" />
The corresponding ClinicFragment java class is given below:
public class ClinicFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public static ClinicFragment newInstance() {
ClinicFragment fragment = new ClinicFragment();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_show_map, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng testLocation = new LatLng(93.972, 26.515);
mMap.addMarker(new MarkerOptions().position(testLocation).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(testLocation));
}
}
Now to trigger map display on button click, I have use the code below:
public void onShowLocationClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.map,new ClinicFragment());
transaction.addToBackStack(null);
transaction.commit();
}
But I am getting the error:
java.lang.IllegalArgumentException: No view found for id 0x7f0e0087 (com.example.dioglgt.myapplication:id/map) for fragment ClinicFragment{9dd7da #0 id=0x7f0e0087}
Please guide in this regard.
Have to use this
public void onShowLocationClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(android.R.id.content,new ClinicFragment());
transaction.addToBackStack(null);
transaction.commit();
}
You are trying to add ClinicFragment on fragment with id map with is inside activity_show_map which is set to Clinic fragment. Try to use frameLayout and replace that frameLayout with your fragment on button click link below:
Fragmentmanager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new ClinicFragment()).commit();
In above code fragment_container is a FrameLayout which will be replaced with you're ClinicFragment;
I have been working in Navigation Drawer Activity, it has many fragments and one of the fragment contains MapFragment.
Now there is a problem in MapFragment i-e When for the first time i open the fragment that contains MapFragment inside it everything works fine, but once i navigate to some other fragment using getFragmentManager().beginTransaction().replace(...)
and then return to previous fragment using getFragmentManager().popBackStack();
My mapFragment.getView() does not works, you can review my code for MapFragment
private void initMap() {
if (mapFragment == null)
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapView);
if (mapFragment == null)
mapFragment = (MapFragment) this.getChildFragmentManager().findFragmentById(R.id.mapView);
if (mapFragment == null) {
Toast.makeText(getActivity(), "mapFragment not initialized", Toast.LENGTH_SHORT).show();
}
if (mapFragment.getView() == null) {
Toast.makeText(getActivity(), "getView not initialized", Toast.LENGTH_SHORT).show();
}
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
Toast.makeText(getActivity(), "map intialized", Toast.LENGTH_SHORT).show();
// For showing a "move to my location" button
CheckPermissions cp = new CheckPermissions(getActivity());
if (cp.getPermission(Manifest.permission.ACCESS_COARSE_LOCATION) || cp.getPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
// enableMyLocation();
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION);
}
mUiSettings = mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mUiSettings.setCompassEnabled(true);
cameraListener();
markerListener();
mapListener();
}
});
}
above method is called if following method
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initMap();
if (mapFragment.getView() != null) {
mapFragment.getView().setVisibility(View.GONE);
}
}
i have searched a lot on every platform but not getting any help.
and i do not want to use SupportFragmentManager because i'm not creating this app for older android versions.
Any help would be appreciable.
Regards.
UPDATE
I have sorted my problem temporarily by putting MapFragment inside RelativeLayout and hiding this RelativeLayout
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initMap();
relativeLayout = (RelativeLayout) view.findViewById(R.id.rel_layout);
if (relativeLayout != null) {
relativeLayout.setVisibility(View.GONE);
}
}
I think your first line of code is what makes your fragment disappear.
Delete this:
if (mapFragment == null)
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapView);
Whenever you are adding a fragment as child to existing fragment, you want to use getChildFragmentManager().
EDIT:
To show / hide / remove child fragment, you can use one of the following methods:
getChildFragmentManager().beginTransaction().hide(fragment).commit();
getChildFragmentManager().beginTransaction() has various methods for managing fragments, such as show / remove / replace / add, etc.
You can try these, though I'm not sure why you wish to hide MapFragment immediately after creating it.
On some devices map is right, but on the some devices gray screen shows instead the map
FragmentManager fm = getFragmentManager();
mapFragment = (MapFragment) fm.findFragmentById(R.id.map_container);
if (mapFragment == null) {
mapFragment = MapFragment.newInstance();
Log.d("CHECK_SPEED", "replace");
fm.beginTransaction().replace(R.id.map_container, mapFragment)
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out)
.commit();
setupGoogleMap();
}
I am using Sliding Tabs with different fragments. In map fragment I cannot use Nested Fragment as XML. Below is Java code and XMLfile.
I am stuck at getMapAsync() method So How Can I get Map using getMapAsync without Any exception? I will really appreciate your cooperation.
Food Fragment Java:
public class FoodFragment extends Fragment {
private SupportMapFragment mapfragment;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.food_layout, null);
mapfragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.maps_frame);
if (mapfragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mapfragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.maps_frame,mapfragment).commit();
}
mapfragment.getMapAsync((OnMapReadyCallback) getActivity().getApplicationContext());
return view;
}
}
Food XML file,
<FrameLayout
android:layout_width="match_parent"
android:layout_height="2in"
android:id="#+id/maps_frame"
android:background="#color/colorAccent"
android:layout_alignParentBottom="true">
</FrameLayout>
Your ApplicationContext cannot be casts as OnMapReadyCallback you can create new instance of OnMapReadyCallback() as an anonymous class instead like here is what you might be searching for
mapfragment.getMapAsync(new OnMapReadyCallback() {
#Override public void onMapReady(GoogleMap googleMap) {
if (googleMap != null) {
// your additional codes goes here
.....
}
}
You can refer this example as well if you need.
I have a google maps activity that extends FragmentActivity in my app and work fine (is just the code that add android studio when create a google maps project):
public class MyMap extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private String TAG = "MAIN ACTIVITY";
protected static String CHECK_PERMISSION = "check.permission";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_beach_map);
String appVersion = "v1";
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#TargetApi(23)
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
addMarker();
getUserCurrectLocation(mMap);
}
.
.
.
Mymap xml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
tools:context="mypackage.com"
android:name="com.google.android.gms.maps.SupportMapFragment" />
In my mainActivity I'm using navigationDrawer and I'm refreshing the main layout with a FrameLayout that is working fine, too.
My problem comes when I try to add my google map class in the frame layout. I'm doing this to set the different fragments in the FrameLayout:
public void setFragment(int position) {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
switch (position) {
case 0:
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
FirstFragment inboxFragment = new FirstFragment();
fragmentTransaction.replace(R.id.flContent, inboxFragment);
fragmentTransaction.commit();
break;
case 1:
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
MyMap starredFragment = new MyMap();
fragmentTransaction.replace(R.id.flContent, starredFragment);
fragmentTransaction.commit();
break;
}
}
But, here I'm getting a wrong 2ยบ argument type. Found: 'mypackage.com', required: 'android.support.v4.app.Fragment':
fragmentTransaction.replace(R.id.flContent, starredFragment);
It's possible to add to the fragmentTransaction a FragmentActivity or is another way to can add my map into the FrameLayout?? I'm searching here but I can't find any question like this....