Andrey Dobrikov
Andrey Dobrikov

Reputation: 457

Android drawer and google map

Hi after endless forums and tries i guess i will need more help.

I need to combine two features in my app: 1. Android drawer. 2. Google map.

The idea is that the "main" screen of my app is a map, and the drawer will help me to change to settings, personal files, etc...

So the question is what is the best way to combine them? I mean i don't want to reopen the map each time I return to map(I have a database for the map and i dont want to reload it every time). And i want the drawer to appear through all screens.

Any ideas?

My code so far: (except other fragments which are blank pages with their names) Main activity xml:

  <?xml version="1.0"?>
    <android.support.v4.widget.DrawerLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/drawer_layout" xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- <?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"/> -->
    <FrameLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/content_frame"/>
    <ListView android:layout_height="match_parent" android:layout_width="240dp" android:id="@+id/left_drawer" android:background="#fff" android:dividerHeight="0dp" android:divider="@android:color/transparent" android:choiceMode="singleChoice" android:layout_gravity="start"/>
    </android.support.v4.widget.DrawerLayout>

Main activity class:

    //import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
//import com.google.android.gms.maps.model.CameraPosition;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;

public class MapActivity extends Activity{

    public static FragmentManager fragmentManager;
    Fragment map = null;
    String[] menu;
    DrawerLayout dLayout;
    ListView dList;
    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        fragmentManager = getFragmentManager();
        menu = new String[]{"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us"};
        dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        dList = (ListView) findViewById(R.id.left_drawer);
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
        dList.setAdapter(adapter);
        dList.setSelector(android.R.color.holo_blue_dark);
        dList.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                dLayout.closeDrawers();
                Bundle args = new Bundle();
                args.putString("Menu", menu[position]);
                Fragment detail = new DetailFragment();

                    if(position == 0){
    //                      if(map == null){
                                map = new MyMapFragment();
    //                      }
                        detail = map;
                    }
                    detail.setArguments(args);
                    FragmentManager fragmentManager = getFragmentManager();
           fragmentManager.beginTransaction().replace(R.id.content_frame,detail).commit();
                    }
            });
        }
    }

Map fragment xml:

<?xml version="1.0"?>

<fragment android:layout_height="match_parent" android:layout_width="match_parent" class="com.google.android.gms.maps.MapFragment" android:id="@+id/map"/>

</RelativeLayout>

Map fragment class:

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

    public class MyMapFragment extends Fragment{

    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private GoogleMap googleMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rooView = inflater.inflate(R.layout.fragment_mymap, container, false);
        setUpMapIfNeeded();
        return rooView;
    }

    private void setUpMapIfNeeded() {
        if(googleMap == null){
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        }
        setUpMap();
    }

    private void setUpMap(){
        Marker hamburg = googleMap.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
        Marker kiel = googleMap.addMarker(new MarkerOptions()
        .position(KIEL)
        .title("Kiel")
        .snippet("Kiel is cool")
        .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher)));
        // Move the camera instantly to hamburg with a zoom of 15.
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

        // Zoom in, animating the camera.
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(googleMap != null){
            MapActivity.fragmentManager.beginTransaction().remove(MapActivity.fragmentManager.findFragmentById(R.id.map)).commit();
            googleMap = null;
        }
    }

Upvotes: 1

Views: 453

Answers (1)

Andrey Dobrikov
Andrey Dobrikov

Reputation: 457

I was able to solve this problem by using 2 activities: 1 - Drawer with a map. 2- Drawer with frames for other windows. Both activities inherit some basic Drawer class.

Upvotes: 1

Related Questions