i want to put google maps in a tab using viewPager ,thus viewPager uses fragments, i found multiple solutions that use the deprecated getMap, so android studio wouldn't even compile it, is there any up to date solution to get the map with viewPager ?
In onCreateView in the mapsActivity.java that extends android.support.v4.app.Fragment and implements OnMapReadyCallback :
View rootView =inflater.inflate(R.layout.activity_maps, container, false);
Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Related
I'm creating programmatically a Huawei map, but despite MapFragment() is a child of Fragment class, the transaction add doesn't recognize it as a Fragment. Here's my code:
val transaction: FragmentTransaction = activity.supportFragmentManager.beginTransaction()
val mapFragment = MapFragment()
transaction.add(this.frame.id, mapFragment) ---> here is the problem
transaction.commit()
Someone knows the reason?
There are 2 different classes for showing map in fragment:
MapFragment
SupportMapFragment
You must use correct one your activity.
If Activity is just Activity and you use just FragmentManager - use MapFragment
If activity is AppCompatActivity and you use SupportFragmentManager - use SupportMapFragment
I am struggling with animateCamera() method of the Google Maps. It only works on first launch of activity and if the activity is destroyed and created again, camera animations doesn't work but map is loaded fine. I have tried debugging the code, everything gets executed but map doesn't animate without any error or log. Although animation works when same mapFragment is used in the fragment but in activity it doesn't seem to work.
The solution mentioned in this this question is deprecated now and I am unable to fix this issue.
Map Fragment
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mapFragment.setRetainInstance(true);
#Override
public void onMapReady(GoogleMap googleMap) {
if (mMap == null) mMap = googleMap;
}
Code for animating camera
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, ZOOM_LEVEL));
Making entire project again from old code fixed the issue. But this issue re-appeared in another app. I found out that application was using old reference to mMap. This can be fixed by either getting reference from fragment manager or just setting mMap = null when activity is destroyed.
I am trying to embed Google map in Activity. I integrated it a couple of times before but It's not happening right now.
My layout file:
<fragment
android:id="#+id/location_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|center_vertical" />
Map Class
#EActivity(R.layout.location_activity)
public class FIndLocationActivity extends AppCompatActivity implements OnMapReadyCallback {
#AfterExtras
void afterViewCreated() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.location_map);
mapFragment.getMapAsync(this);
.....
.....
}
}
mapFragment remains always Null. What am I doing wrong here? I also followed this tutorial but still it's throwing null. I am using actual device to test the app.
Check if you added all credentials for GMaps in your app, including additional egl 'uses' lines in manifest. This may be the issue why system can't create fragment.
I have a viewpager where each "page" is a fragment. Each of the "page fragments" have a map fragment (google maps sdk for android) inside, added programatically using getSupportFragmentManager (If I use getFragmentManager it doesn't work either). Each added map is a new instance of SupportMapFragment (MapFragment doesn't work either).
On the first fragment, the one that is first added to the viewpager, the map works. At the other fragments, the non-interactive map just show itself as a grey square.
I'm using the 'com.google.android.gms:play-services-maps:7.5.0' dependency.
All the added maps are using the lite version. I'm doing this with:
GoogleMapOptions options = new GoogleMapOptions();
options.liteMode(true);
options.mapToolbarEnabled(false);
SupportMapFragment mapFragment = SupportMapFragment.newInstance(options);
I've read on some blogspots and here on Stackoverflow that maybe an app can only have one instance of google maps per process. Have anybody faced the same problem? Is my guess correct?
Thanks.
If you want to put a fragment into an other fragment, use getChildFragmentManager() instead of getSupportFragmentManager()
getChildFragmentManager
Return a private FragmentManager for placing and managing Fragments
inside of this Fragment.
Map fragment will be the default fragment and on a click a new fragment should be loaded and a value has to be entered in the fragment and this value has to be passed to the map fragment
To use MapFragment in Fragment Follow this link
using the Support Library to work (SupportMapFragment instead of MapFragment), so that the MapFragment can even work on below Android 3.0. You can replace the SupportMapFragment with the MapFragment if you are working on Android 3.0 or above.