Load Google Map in Fragment / swipeable Tab - android

i am working with swipeable tabs and want to display a mapsfragment inside the second tab, but everytime i try to get access to the map, there appears a nullpointerexception. Following the code of my Fragment/Tab.
public class tab2_advanced extends Fragment implements OnMapReadyCallback{
private GoogleMap gmap;
private static View view;
MapView ourMap;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab2_erweitertezusage, container, false);
Log.i("TAB2", "Start.");
//without the following lines regarding null or not null, the fragment loads the map, but i want to set markers and select a position on the map.
if (gmap==null) {
Log.i("Tab2", "Map null");
gmap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
gmap.setMyLocationEnabled(true);
}
if (gmap != null) {
Log.i("Tab2", "Map not null");
gmap.setMyLocationEnabled(true);
}
return v;
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.i("Aufruf onMapReady", "Darstellung"+gmap);
//Später Übergabe der Pos aus DB
LatLng sidney= new LatLng(49.7685, 9.9382);
gmap.setMyLocationEnabled(true);
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(wuerzburg, 13));
gmap.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(wuerzburg));
}
My XML-File for this is the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp">
<LinearLayout
android:orientation="vertical"
android:id="#+id/layoutMap"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
Without the described part in comments in the code of the fragment, the map loads the content. There are still some parts missing in the case, if the map is not null, but i tried it and it lands everytime in null with a following NullpointerException.
Hoping for hints :)
Greetings

Okay, i solved the issue by:
if (gmap==null) {
Log.i("Tab2", "Map null");
gmap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
gmap.setMyLocationEnabled(true);
}
if (gmap != null) {
Log.i("Tab2", "Map not null");
gmap.setMyLocationEnabled(true);
gmap.getUiSettings().setZoomControlsEnabled(true);
actLocation.getLocation(getActivity(), locationResult);
LatLng actPos = new LatLng(organizerGPS.latitude, organizerGPS.longitude);
Log.i("tab2", "actPos: " + actPos);
gmap.setMyLocationEnabled(true);
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(actPos, 13));
...
The hint with the getChildFragmentManager was good, but the reason for the NullpointerException was, that the view was null and that results to the error. Now the initialisation of the map is right behind setting the view and in addition i added a test.

Related

MapFragment is displaying but can't add markers, move camera, etc. with Google Map

I'm making an application using Android Studio and even though I've got the map and it's being displayed, I can't add markers or do anything else to it using the GoogleMap object obtained from OnMapReady.
Here's the code from my Main Activity:
public class MainActivity extends AppCompatActivity implements OnFragmentInteractionListener, OnMapReadyCallback {
MapFragment mapFragment;
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchFragments(GlobalConsts.FRAGMENT.MAP);
}
public void switchFragments(GlobalConsts.FRAGMENT fragment){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (fragment) {
case MAP:
if (mapFragment == null) {
mapFragment = new MapFragment();
}
mapFragment.getMapAsync(this);
transaction.replace(R.id.fragmentContainer, mapFragment);
transaction.addToBackStack(null);
break;
}
transaction.commit();
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions()
.position(sydney)
.title("Sydney")
.snippet("Population: 4,627,300")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
Here's the xml layout for my Main Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
tools:context="jarett.familymap.MainActivity">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
My MapFragment class:
public class MapFragment extends SupportMapFragment {
public MapFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_map, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
and the xml layout for my MapFragment in fragment_map.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mapFragment"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
tools:context="jarett.familymap.MapFragment">
</fragment>
Like I said, everything seems to be working fine as far as displaying the map fragment but for the life of me I can't figure out why the code for zooming and adding markers isn't doing anything. I appreciate the help.
I can't figure out why the code for zooming and adding markers isn't doing anything
Well, you have two maps. Your MapFragment extends SupportMapFragment, so that will give you one map. Then, your MapFragment inflates a layout that, in turn, creates a SupportMapFragment. This gives you two maps.
Either:
Delete MapFragment entirely, along with its associated layout file, and just have your activity create a SupportMapFragment, or
Delete your onCreateView() method from MapFragment, and move your map-management code into MapFragment, triggered by a getMapAsync() call in onViewCreated() (as I worry that you are calling it too soon in your existing code)

Google maps error inflating, Android

I get the following error when I try to leave fragment which contains map (on back button press).
ERROR: mapInflater->Binary XML file line #7: Error inflating class fragment
Bellow is onCreate() method of fragment where the map is placed. And also there is a xml code of layout.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
apiRequestManager = ApiRequestManager.getInstance();
fragmentHandler = FragmentHandlerActivityMain.getInstance(getActivity());
session = new SessionManager(App.getInstance().getApplicationContext());
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.maps_fragment_toolbar_name));
Handler mainHandler = new Handler();
Runnable myRunnable = new Runnable() {
#Override
public void run() {
try {
SupportMapFragment maps = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.mapView);
if (maps != null)
maps.getMapAsync(MapsFragment.this);
} catch (Exception ex) {
LogUtil.getInstance().add(LogUtil.ERROR,
TAG, "onCreateMaps", ex);
}
}
};
mainHandler.post(myRunnable);
}
Log is pointing on this layout on fragment (line 7).
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app1="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".salesman.fragments.MapsFragment">
<fragment
android:id="#+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
</FrameLayout>
You are trying to getChildFragmentManager() instade of getSupportFragmentManager().
Need to change In Java File :
1). You need to implements OnMapReadyCallback in Your Fragment.
2). Get SupportMapFragment from View.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
3). Overrid onMapReady(GoogleMap googlMap) Method.
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
// and move the map's camera to the same location.
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
In XML File :-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app1="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".salesman.fragments.MapsFragment">
<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"/>
</FrameLayout>
Please check the updated answer as per below comment:
Now try to set MapView insted of fragment because if you use fragment inside fragment it creates lot of issues.
Need to change in JAVA Code : // Initialize GoogleMap myMap; MapView mMapView;
// OnCreateView Method MapsInitializer.initialize(this.getActivity()); mMapView = (MapView) rootView.findViewById(R.id.map); mMapView.onCreate(savedInstanceState); mMapView.getMapAsync(this);
#Override public void onPause() { super.onPause(); mMapView.onPause(); }
#Override public void onPause() { super.onPause(); mMapView.onPause(); }
#Overridepublic void onResume() { super.onResume(); mMapView.onResume(); }
Need to change in XML File :
<com.google.android.gms.maps.MapView android:id="#+id/map" android:layout_width="match_parent" android:layout_height="match_parent" />

Maps API Fragment not showing Marker in DrawerLayout

I'm new with StackOverflow and Maps API. I'm developing an app where a Drawer Layout is being used. I want to use one of the Drawer fragments to show Maps and work with Markers.
private SupportMapFragment mapFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapFragment = (SupportMapFragment)
getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_maps, container, false);
mapFragment.getMapAsync(this);
return view;
}
#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));
}
this is the fragment_maps.xml
<FrameLayout 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.labcontrol.lca.view.MapsFragment">
<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"
tools:context="com.labcontrol.lca.controller.MainActivity" />
</FrameLayout>
In the official guide they made an example using an Activity, as you can see I tried to do it inside a fragment. is it possible?
The result is this: maps is working properly, but I can't see the Marker I created, and I spent too many hours trying to figure out why.
The only thing make me suspicious is this error when playing with Maps on the phone and changing the camera:
W/Google Maps Android API: GLHudOverlay deprecated; draw(): no-op
Found out it's a common issue these days, a very new bug. Somebody knows how to get it up and running? Is there something wrong with the code?
thanks a lot

To overlay two buttons on Google map such that each button click shows different set of markers on the map. Can this be done?

I need to add two buttons that overlays on the google map such that each click shows a different set of markers on the map. Is this achievable or do I need to change the approach?
My Fragment code:
public class HomeFragment extends Fragment {
public HomeFragment(){}
static final LatLng VELACHERY = new LatLng(12.9758, 80.2205);
static final LatLng TAMBARAM = new LatLng(12.9300, 80.1100);
private GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
//creating Markers
Marker hamburg = map.addMarker(new MarkerOptions().position(VELACHERY)
.title("Velachery"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(TAMBARAM)
.title("Volunteer")
.snippet("I am safe")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(VELACHERY, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
//map.setOnMarkerClickListener(listener)
return rootView;
}
public void onDestroyView() {
super.onDestroyView();
if (map != null) {
getFragmentManager()
.beginTransaction()
.remove(getFragmentManager().findFragmentById(R.id.map))
.commit();
}
}
}
My Fragment layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
Yes, it's possible.
You could use a MapView or MapFragment-SupportMapFragment in a FrameLayout with the layout that contains your two buttons
Use OnMapReadyCallback to get the Google Map instance getMap is deprecated - look for getMapAsync(OnMapReadyCallback)
Handle View.OnClickListener for buttons
Each time a button is pressed call GoogleMap.clear(); then add the markers you need to display for the button that is pressed
Layout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<Button
android:id="#+id/buttonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A"/>
<Button
android:id="#+id/buttonB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button B"/>
</LinearLayout>
</FrameLayout>
Code
buttonA = (Button)findViewById(R.id.buttonA);
buttonA.setOnClickListener(this);
buttonB = (Button)findViewById(R.id.buttonB);
buttonB.setOnClickListener(this);
mapView = (MapView)findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
Implement OnMapReadyCallback.onMapReady
#Override
public void onMapReady( GoogleMap googleMap )
{
this.googleMap = googleMap;
//Do any other setup you would like here
}

Android map in fragment nullpointer

In my project im trying to implementthis solution for managing a map in a dynamic added fragment.
public class MapFragment extends Fragment {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.fragment_map, container, false);
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_arrow)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(KIEL, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
return v;
}
}
My view:
<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"
android:background="#color/ColorBackground"
tools:context="com.example.sven.questy.GameFragments.MapFragment">
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
WHen i run the app i get a nullpointer exception on:
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
The id of the map = map ( android:id="#+id/map").
Why the nullpointer?
Since you are using nested fragments, you should use getChildFragmentManager()
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
And I recommend you to use getMapAsync method, because getMap is deprecated
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
setupMap(googleMap);
}
});
You need to use findViewById on the view you get, not using the fragment manager. The map is attached to the view
View v =inflater.inflate(R.layout.fragment_map, container, false);
map = v.findViewById(R.id.map))
.getMap();

Categories

Resources