How can Android Activity pass a coordinate value to a Fragment? - android

I have an ActionBar and it has Tabs.
In the MainActivity there is a LocationListener implemented.
In onLocationChanged(Location location) method I save the longitude value to double longitude variable.
I would like to pass this value to the RunFragment class.
Could anybody help me?
This is the MainActivity code:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener, LocationListener{
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
double longitude;
private LocationManager lm;
#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().setText("Run").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Map").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Statistics").setTabListener(this));
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
});
protected void startLocationUpdate() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
public void onLocationChanged(Location location) {
longitude=location.getLongitude();
}
This is the RunFragment code:
public class RunFragment extends Fragment {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.run_layout, container, false);
tv =(TextView)view.findViewById(R.id.textView1);
return view;
}
public void setTv(String string){
tv.setText(string);
}
}

Can't you get a reference to the RunFragment on the Activity through a findViewById()? Then add a method to RunFragment and call it from onLocationChanged().

Related

android - Add a Marker to a Fragment Map from Activity

I need to Add a Mark to a Android Google Map (Fragment) from a MainActivity
This is my code;
class Map extends android.app.Fragment implements OnMapReadyCallback
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_mapa, container,false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(19.33978502, -99.19086277);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
googleMap.addMarker(new MarkerOptions().title("Marca de Prueba 1").position(marker));
}
}
And i want to add a mark from here:
All this because i want to interact with my map from my main Activity where i have some buttons and EditText
MainActivity
public class MainActivityextends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_side_bar);
}
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new Mapa()).commit();
}
Here is how it looks, already has a mark, but insted of calling it from the map activity i need to set it from the MainActivity
You just need to define a public method in the Fragment that can be called from the Activity:
class Mapa extends android.app.Fragment implements OnMapReadyCallback
GoogleMap mMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_mapa, container,false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
//Added public method to be called from the Activity
public void placeMarker(String title, double lat, double lon) {
if (mMap != null) {
LatLng marker = new LatLng(lat, lon);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
mMap.addMarker(new MarkerOptions().title(title).position(marker));
}
}
}
Then, in the Activity, keep a reference to the Fragment so that you can call the placeMarker() method:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Mapa mMapFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_side_bar);
}
mMapFragment = new Mapa();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, mMapFragment).commit();
}
private void placeMarkerInMap(String title, double lat, double lon) {
if (mMapFragment != null) {
mMapFragment.placeMarker(title, lat, lon);
}
}
}
Then you can call the placeMarkerInMap() method in your Activity when the user fills out the EditText and clicks the button.
Note that if you try to call this method before the onMapReady() callback executes, there will not be a valid GoogleMap reference to use to place the Marker.
If you need to place a Marker on initial launch from the Activity, you'll need to use arguments in the FragmentTransaction. See here for more details.

Add markers on Google Map in other Fragment

How to get access from main activity to fragment? I want to add marker in fragment class with location from recycledview. Object with location data is in ClubBean. I obtained this by interface ClubAdapter.OnClubClickListener:
#Override
public void onClicked (ClubBean club) {
ClubBean bean = club;
Log.d("Name: ", bean.getClubName());
}
Main Activity:
public class MapsActivity extends FragmentActivity implements LoadAllClubsInterface, ClubAdapter.OnClubClickListener {
private DrawerLayout drawerLayout;
private RecyclerView clubRecycler;
private RecyclerView.LayoutManager clubLayoutManager;
private ArrayList<ClubBean> clubList = new ArrayList<ClubBean>();
private RecyclerView.Adapter clubAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new MyMapFragment()).commit();
new LoadAllClubs(this).execute(); //load list in background from database
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
clubRecycler = (RecyclerView) findViewById(R.id.recycler_view);
clubRecycler.setHasFixedSize(true);
clubLayoutManager = new LinearLayoutManager(this);
clubRecycler.setLayoutManager(clubLayoutManager);
}
#Override
public void finishDataLoad(ArrayList<HashMap<String, String>> clubs) {
Iterator<HashMap<String, String>> iterator = clubs.iterator();
Map<String, String> map = new HashMap<String, String>();
while (iterator.hasNext()){
map = iterator.next();
clubList.add(new ClubBean(map.get("name"),map.get("localization"), map.get("score")));
}
//pass the class that implements your listener as a parameter.
clubAdapter = new ClubAdapter(clubList, this, this);
clubRecycler.setAdapter(clubAdapter);
}
#Override
public void onClicked (ClubBean club) {
ClubBean bean = club;
Log.d("Name: ", bean.getClubName());
}
}
My MapFragment Class:
public class MyMapFragment extends Fragment implements OnMapReadyCallback{
GoogleMap mGoogleMap;
MapView mMapView;
View mView;
public MyMapFragment(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.map_fragment, container, false);
return mView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView = (MapView) mView.findViewById(R.id.map);
if (mMapView != null){
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getActivity().getApplicationContext());
mGoogleMap = googleMap;
LatLng triCity = new LatLng(54.4158773,18.6337789);
mGoogleMap.addMarker(new MarkerOptions().position(triCity).title("Trojmiasto"));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(triCity, 11));
}
}
The convenient way of communication between Activity and Fragment is via Interface. Here is example code based on your problem:
Step 1: Define an interface like this:
public interface IFragmentController{
void passDataToFragmentMethod(String someStringValue);
}
Step 2:Implement this Interface into your fragment. Then you will get passDataToFragmentMethod(String someStringValue) method in your fragment.
public class MyMapFragment extends Fragment implements IFragmentController{
#Override
void passDataToFragmentMethod(String someStringValue){
// So your logic code here using passed value
}
}
Step 3: In your Activity just get instance of your fragment and call passDataToFragmentMethod method in this way:
Fragment mapFragment=new MyMapFragment();
and
#Override
public void onClicked (ClubBean club) {
ClubBean bean = club;
Log.d("Name: ", bean.getClubName());
mapFragment.passDataToFragmentMethod(bean.getClubName());
}
Hope this will help you to solve your problem :)
The host activity can deliver messages to a fragment by capturing the Fragment instance with findFragmentById(), then directly call the fragment's public methods.
https://developer.android.com/training/basics/fragments/communicating.html
Just define a method in MyMapFragment like addMarkers, then call it from activity.
You can do something like this
public class MapsActivity extends FragmentActivity implements LoadAllClubsInterface, ClubAdapter.OnClubClickListener {
MyMapFragment mMyFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mMyFragment=new MyMapFragment();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_frame,mMyFragment).commit();
}
private void addMarkers(){
//defined this method in Mapfragment
mMyFragment.addMarkers();
}
if u have list then
For(ClubBean obj:ClubList)
{
// latitude and longitude
double latitude =obj.getLatValue();
double longitude = obj.getLngValue();
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
// adding marker
googleMap.addMarker(marker);
}

Map in fragment Android result in error

i try to create a map in a fragment in a swipe view, but with this code i have a error in logcat, this is the code:
public class Mapa extends Fragment {
private static final double MARKER_LATITUDE = 42.027325;
private static final double MARKER_LONGITUDE = -8.640842;
GoogleMap map;
private FragmentActivity myContext;
public Mapa() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static Mapa newInstance() {
Mapa fragment = new Mapa();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.mapa, container, false);
final LatLng position = new LatLng(MARKER_LATITUDE, MARKER_LONGITUDE);
// camera position
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener()
{
#Override
public void onCameraChange(CameraPosition arg0)
{
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(position, 17);
map.animateCamera(cu);
map.setOnCameraChangeListener(null);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
});
return android;
}
#Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
#Override
public void onDestroyView() {
super.onDestroyView();
Fragment f = getFragmentManager().findFragmentById(R.id.mapFragment);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
}
and this is the logcat error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnCameraChangeListener(com.google.android.gms.maps.GoogleMap$OnCameraChangeListener)' on a null object reference
at greetrack.estg.ipvc.greentrack.Mapa.onCreateView(Mapa.java:49)
its my first application using maps, maybe somethings is wrong
You havent setup map, its null. So the null reference.
Map = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
is one way, or potentially following googles recommendation. Here is a maps fragment xml.
The xml file i put in isn't showing https://developers.google.com/maps/documentation/android/start#get_an_android_certificate_and_the_google_maps_api_key here is the linkk to it.
Add the following code in MainActivity.java.
package com.example.mapdemo;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

How to implement google map in fragment

I have a MainActivity which has an ActionBar with two tab, the first tab is to show the sensor data value and the second tab I want to show the Map.
But I have a problem to implement the map in the fragment on the second tab
The MainActivity is shown as below:
public class MainActivity extends Activity
{
private static final String DATA_FRAGMENT_BAR = "Data";
private static final String MAP_FRAGMENT_BAR = "Map";
private DataCollection mDataCollection;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Config.mContext = this;
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME| ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab dataTab = actionBar.newTab().setText(DATA_FRAGMENT_BAR);
ActionBar.Tab mapTab = actionBar.newTab().setText(MAP_FRAGMENT_BAR);
Fragment dataFragment = new FragmentData();
Fragment mapFragment = new FragmentMap();
dataTab.setTabListener(new MyTabsListener(dataFragment));
mapTab.setTabListener(new MyTabsListener(mapFragment));
actionBar.addTab(dataTab);
actionBar.addTab(mapTab);
mDataCollection = DataCollection.getInstance();
}
....
}
The TabsListener is shown as below
public class MyTabsListener implements ActionBar.TabListener
{
private Fragment fragment;
public MyTabsListener(Fragment fragment)
{
this.fragment = fragment;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.fragment_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(fragment);
}
}
The the map fragment class is shown as below:
public class FragmentMap extends Fragment
{
MapView mapView;
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_map, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
//map.getUiSettings().setMyLocationButtonEnabled(false);
//map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(new LatLng(50.167003,19.383262)));
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try
{
MapsInitializer.initialize(this.getActivity());
}
catch (GooglePlayServicesNotAvailableException e)
{
e.printStackTrace();
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
return v;
}
#Override
public void onResume()
{
mapView.onResume();
super.onResume();
}
#Override
public void onDestroy()
{
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory()
{
super.onLowMemory();
mapView.onLowMemory();
}
}
I set a break point in the FragmentMap class at the line "mapView.onCreate(savedInstanceState);" the savedInstanceState is null and the program will crash after line.
Can someone help. Thanks in advance.
Google released the Map API Version 2. This gives you a MapFragment and a SupportMapFragment. This allows you to add a Map without extending MapActivity.
Google Maps v2 In fragment cashes when clicked twice
It is not really clear in the documentation, but the MapView.onCreate() method should be called in the Fragment.onActivityCreated(), not in Fragment.onCreate()
Also, MapView.getMap() can return null, don't forget to check with that.
And you should call super.onResume() before mapView.onResume()

Map in tab becomes frozen after pause & resume

I am using actionbarsherlock to implement a set of tabs. One of the tabs is a fragment containing a MapView. My onCreateView is as follows:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.mapview, container, false);
mMapView = (MapView) view.findViewById(R.id.mapv);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try
{
MapsInitializer.initialize(getActivity());
}
catch (GooglePlayServicesNotAvailableException e)
{
e.printStackTrace();
}
googleMap = mMapView.getMap();
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(PARIS, 10));
googleMap.addMarker(new MarkerOptions().position(PARIS).title("Paris").snippet("Great city"));
return view;
}
As you can see, the map is originally displayed zoomed in on Paris, which has a Marker on it. Everything is working fine so far. I see the map and can manually drag and zoom the map. If I then press the button on the side of my phone to cause it to pause, then unlock the phone to resume, I can still drag and zoom. BUT if I switch to another tab, then come back to the map tab, I see the map as I left it, but it no longer responds to drag/zoom. It appears frozen. But no errors are being displayed in my log and I can still flip between tabs.
Without any log errors, I don't know how to track down the problem.
EDIT: I'm using Maps v2
EDIT: I've just found a similar sounding situation here. It may well be the answer, but I don't know quite where to place the code from the suggested solution.
EDIT: Just in case the critical code was not in my snippet, here's the whole thing...
package com.mycompany.myapp;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.view.View;
public class TestMapFragment extends Fragment implements TabListener
{
private Fragment mFragment;
private MapView mMapView;
private GoogleMap googleMap;
private static final LatLng PARIS = new LatLng(48.874,2.347);
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.mapview, container, false);
mMapView = (MapView) view.findViewById(R.id.mapv);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try
{
MapsInitializer.initialize(getActivity());
}
catch (GooglePlayServicesNotAvailableException e)
{
e.printStackTrace();
}
googleMap = mMapView.getMap();
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(PARIS, 10));
googleMap.addMarker(new MarkerOptions().position(PARIS).title("Paris").snippet("Great city"));
return view;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
mFragment = new TestMapFragment();
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
/*
* Using a mapview in a fragment requires you to 'route'
* the lifecycle events of the fragment to the mapview
*/
#Override
public void onResume()
{
super.onResume();
if (mMapView != null)
{
mMapView.onResume();
}
}
#Override
public void onPause()
{
super.onPause();
if (mMapView != null)
{
mMapView.onPause();
}
}
#Override
public void onDestroy()
{
super.onDestroy();
if (mMapView != null)
{
mMapView.onDestroy();
}
}
#Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
if (mMapView != null)
{
mMapView.onSaveInstanceState(outState);
}
}
#Override
public void onLowMemory()
{
super.onLowMemory();
if (mMapView != null)
{
mMapView.onLowMemory();
}
}
}
This added line container.removeAllViewsInLayout() seems to have done the trick.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
container.removeAllViewsInLayout();
View view = inflater.inflate(R.layout.mapview, container, false);
i also had the same problem
i have done something like this
Fragment activity which will hold the fragments
Map_main_screen.java
public class Map_main_screen extends FragmentActivity {
private static final String[] CONTENT = new String[] { "TAB one",
"Second tab GOOGLE MAP", };
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.map_new);
FragmentPagerAdapter adapter = new GoogleMusicAdapter(
getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
indicator.setViewPager(pager);
==========================new_class_GoogleMusicAdapter============================
class GoogleMusicAdapter extends FragmentPagerAdapter {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return Map_google_TestFragment.newInstance(CONTENT[position
% CONTENT.length]);
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}
#Override
public int getCount() {
return CONTENT.length;
}
}
// =================adapter ends here===============================
}
Map_google_TestFragment.java
public final class Map_google_TestFragment extends Fragment {
private static final String KEY_CONTENT = "TestFragment:Content";
Typeface tf;
public static Map_google_TestFragment newInstance(String content) {
Map_google_TestFragment fragment = new Map_google_TestFragment();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 1; i++) {
builder.append(content).append(" ");
}
builder.deleteCharAt(builder.length() - 1);
fragment.mContent = builder.toString();
return fragment;
}
private String mContent = "???";
private int[] mICONS = { 1 };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null)
&& savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View v;
if (mContent.toString().equals("TAB one")) {
v = vi.inflate(R.layout.YOUR_SECOND_LAYOUT, null);
//***************************************
// function related to your first fragment
//***************************************
} else {
//***************************************
// inflate your Google map layout here
// function related to your Second fragment
//***************************************
View view = inflater.inflate(R.layout.mapview, container, false); //INFLATE GOOGLE MAP LAYOUT
mMapView = (MapView) view.findViewById(R.id.mapv);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try
{
MapsInitializer.initialize(getActivity());
}
catch (GooglePlayServicesNotAvailableException e)
{
e.printStackTrace();
}
googleMap = mMapView.getMap();
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(PARIS, 10));
googleMap.addMarker(new MarkerOptions().position(PARIS).title("Paris").snippet("Great city"));
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
Have you tried using mMapView.displayZoomControls(true) right after mMapView = (MapView) view.findViewById(R.id.mapv)?

Categories

Resources