0

so i'm developping an application with fragments .I have 3 action bars , in one of them i want to display google map in order to allow users to search stores on the map and geolocate then .

My main activity :

    package com.tabdemo;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;

import android.app.ActionBar;
import android.app.Dialog;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.TextView;
import android.widget.Toast;

//


public class MainActivity extends FragmentActivity implements  ActionBar.TabListener{
    ActionBar actionbar;
    ViewPager viewpager;
    FragmentPageAdapter ft;






    @Override
    public boolean onCreateOptionsMenu(Menu menu){

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu,menu);
        return true;




    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        ft = new FragmentPageAdapter(getSupportFragmentManager());

        actionbar = getActionBar();
        viewpager.setAdapter(ft);
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.addTab(actionbar.newTab().setIcon(R.drawable.shop).setTabListener(this));
        actionbar.addTab(actionbar.newTab().setIcon(R.drawable.search1).setTabListener(this));
        actionbar.addTab(actionbar.newTab().setIcon(R.drawable.favorite).setTabListener(this));


        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
            actionbar.setSelectedNavigationItem(arg0);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewpager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
    public Object getDrawerToggle() {
        // TODO Auto-generated method stub
        return null;
    }


}

searchfragment where i displayed my map

    package com.tabdemo;



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.MapView;

import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


import android.os.Bundle;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


public class SearchFragment extends Fragment   {



    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
        // TODO Auto-generated method stub

        return inflater.inflate(R.layout.search_layout, container,false);



    }

searchRFagment XML :

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wallpaper3"
    >



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




</RelativeLayout>
  • It looks like you are trying to add a map to your app on a tab, have you seen the recent answer I posted here? http://stackoverflow.com/questions/28117839/supportmapfragment-issue-in-nested-tabhost-fragment/ – Daniel Wilson Jan 25 '15 at 01:07
  • Hello , thank you so much for your response , but i have a question , what you send me is your whole project ? i need to implement that instead of what i put in the searchfragment ? – Studentgirl Jan 25 '15 at 12:12
  • Yes, you are using the tabbed navigation mode on the action bar in your post. This technique is deprecated and there are various subtle bugs with maps you will encounter. You should use the technique I outlined in that link – Daniel Wilson Jan 25 '15 at 16:39
  • i have succeeded in implementing my app , but now i cant get my current location and i cant do search on it – Studentgirl Jan 25 '15 at 17:11
  • :( I'm afraid these are separate issues though for which you will need to research the problems - you should closely follow the location services lesson - https://developer.android.com/training/location/retrieve-current.html The location client connects in my linked code when the map fragment is made visible (setUserVisibleHint()), using the location manager. Best of luck! – Daniel Wilson Jan 25 '15 at 18:29

1 Answers1

1

You can solve all your problems regarding location and maps if you go through this particular tutorial. It discuss about the following issues:

  • Three Action Bar Tabs as Fragments.
  • Current Location.
  • Maps.

EDIT For getting the current location on map, you have to use the Location Services. Implement the location listener and callbacks associated with them. You will get a latitude and a longitude value which will be your location on the map. For the code implementation please have a look at the following tutorial.

Hope this would Help!!

AniV
  • 3,997
  • 1
  • 12
  • 17
  • thank you for your post , the thing is that me i have that menue installed already , on the map action bar i displayed the google map with a fragment , i couldnt do it other wise but my issue is how can geolocate my self inside the map ? – Studentgirl Jan 26 '15 at 22:47
  • my app is like this one http://stackoverflow.com/questions/16257805/how-to-keep-map-state-on-device-rotation , i displayed the map with thsi command : – Studentgirl Jan 26 '15 at 22:52
  • now on my class that is searchfragment extend fragment i cant do nothing to manipulate my map – Studentgirl Jan 26 '15 at 22:53
  • when i try to implement the same code on my search fragment it doesnt allow me cos it's not an activity@AniV – Studentgirl Jan 26 '15 at 23:07