Android fail to load google map in fragment - android

I'm trying to use google map in fragment, but I meet a problem I don't understand.
I don't arrive to load the map in this line :
gMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
This line seems to return always null...
I tried to use MapFragment inseatd of SupportMapFragment, but it's not possible.
How to resolve it ?
To explain why i want to show it in a fragment, it's because i'm using a navigationdrawer, and one of my item needs to show the map.
thx,
EDIT : My xml :
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>

I think you should call with getsupportFragmentManager not with getFragmentManager otherwise it will return null always

If you are using SupportMapFragment, you need to change your layout!
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
This is a basic example using SupportMapFragment:
public class MainActivity extends ActionBarActivity implements OnMapReadyCallback{
private SupportMapFragment map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map.getMapAsync(this);//remember getMap() is deprecated!
}
#Override
public void onMapReady(GoogleMap map) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(47.17, 27.5699), 16));
map.addMarker(new MarkerOptions() .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)).anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
.position(new LatLng(47.17, 27.5699))); //Iasi, Romania
map.setMyLocationEnabled(true);
}
}
and change the reference in your layout:
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />

I dont think it is possible as google map contains a < fragment> tag. See here why:
Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically.
Some people in SO however managed to show map in a fragment, but the solution was hacky.

Related

Google Maps Display

Iam practicing on Google Maps for android development. I have followed the steps as in https://developers.google.com/maps/documentation/android-api/map-with-marker
While when playing the app I got the screen like this below, no marker and the map cannot show any thing
My code as following:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
******** . XML Layout ************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ae.map_04.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
This is most likely an API Key issue. You can find some guidance here: Android Studio - Google map still blank on real Android device on release apk
If that does not work, try a new API Key.

Android Google Map Activity recreates after Back Button was pressed

I am using Google Map in my project, and I created an Activity with the map fragment inside. After map was successfully loaded, if I swipe a little bit and hit Back button quickly (May be when the map is still moving), the Map Activity will reload itself. I tracked its life cycle methods and found that it restarted from OnCreate method. This occurs sometimes, not always.
If I change the launch mode of the activity to "singleTask", it works fine. However, it is not recommended to use singleTask Mode generally, based on official documentation.
Can anyone please help to explain what happens under this scene and provide the best way to fix it? Thanks.
The XML of the activity is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.alcatel.movetime.ui.view.map.MapActivity"
android:clickable="true">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</RelativeLayout>
And the Activity is also pretty simple:
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
final LatLng kidPosition = new LatLng(31.2, 121.5);
CameraPosition target;
target = CameraPosition.builder().target(kidPosition).zoom(14).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(target));
}
}

Can't find google maps fragment

I have been stuck with creating map inside a tab. getActivity().getSupportFragmentManager().findFragmentById(R.id.map)
returns null. Even if I replace it with getChildFragmentManager().findFragmentById(R.id.map).
I am fairly new to this, so I might have something wrong elsewhere, but I have no idea what it could be.
code
public class FragmentMap extends Fragment implements OnMapReadyCallback
#Override
public void onStart() {
super.onStart();
mMapFragment = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);
}
xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
change
android:name="com.google.android.gms.maps.MapFragment"
to
android:name="com.google.android.gms.maps.SupportMapFragment"

Camera Update Factory is Not Initiliaze in Google Map

i had a map in my android app.....me and my friend is working on that he had created a map nd it is display in his emalutor...when i import same project in my eclipse ..nd run it gives me error that camera update factory is not initilze...but my partner is working on same map....i cant understand what is the problem in my emalutor or eclipse or any other reason..if any one know then help me
my xml file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
i had called this from my map activity
MapsInitializer.initialize(this);
getLayoutInflater().inflate(R.layout.activity_main_map, frameLayout);
googleMap = supportMapFragment.getMap();
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20.0f));
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
The getMap() method is async , your next line will be executed before the map is ready , do this:
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20.0f));
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
}
}

GoogleMap throwing null pointer on getMap();

I am having a FragmentActivity, where I have successfully implemented a Google Map through my XML with this code:
<fragment
android:id="#+id/googleMap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
However, if I try to initialize the map to find a persons location with this code:
GoogleMap googleMap;
googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.googleMap))).getMap();
It is throwing a null pointer exception, as I have understood from other questions regarding this error, it shouldn't be a problem initializing the map when it is created in the XML file. And yes I have used
setContentView(R.layout.map);
Before I initialize the map.
You use the Support Fragment So in your XML File Add
this Line In Fragment
class="com.google.android.gms.maps.SupportMapFragment"
like belove.
<fragment
android:id="#+id/googleMap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
and Put This
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.googleMap)).getMap();
The error is due to incorrect variable initialization try to change your code as shown below,thanks
private GoogleMap gMap;
MapFragment googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_search);
googleMap= (MapFragment) getFragmentManager().findFragmentById(
R.id.googleMap);
gMap= googleMap.getMap();
In you xml you are using MapFragment.Change it -
<fragment
android:id="#+id/mapfragment"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
class="com.google.android.gms.maps.SupportMapFragment" />
java -
googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();

Categories

Resources