0

I am trying to switch fragments after login, I have MainActivity that for the first time add the LogIn Fragment to his content, after the user login, I want to switch to a Google map Fragment.

How can I create a Google Map class extending Fragment and not FragmentActivity or Activity?

Can I add FragmentActivity to the layout of the MainActivity?

user1684140
  • 444
  • 6
  • 18

2 Answers2

1
 /**
* Fragment that appears in the "content_frame", shows a planet
 */
  public class YS_MapFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";

public YS_MapFragment() {
    // Empty constructor required for fragment subclasses
}

MarkerOptions markerOptions;
LatLng latLng;
String locationString;
MapView mapView;
GoogleMap googleMap;
private String tag = "TAG";
private String msg = "= ";
// GPSTrackerN class
GPSTracker gps;
double latitude = 0.0, longitude = 0.0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,      Bundle savedInstanceState) {
    View mainLayout = inflater.inflate(R.layout.fragment_map, container,false);
    locationString = getArguments().getString("location");    
    latitude = getArguments().getDouble("Lat");    
    longitude = getArguments().getDouble("Long");    
    // Gets the MapView from the XML layout and creates it
    mapView = (MapView) mainLayout.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    googleMap = mapView.getMap();
    googleMap.getUiSettings().setMyLocationButtonEnabled(false);
    googleMap.setMyLocationEnabled(true);

    // Needs to call MapsInitializer before doing any CameraUpdateFactory
    // calls
    MapsInitializer.initialize(this.getActivity());

    if (locationString != null && !locationString.equals(""))
    {
        getLocation(latitude, longitude);

    } else {
        getCurrentLocation();
    }
    return mainLayout;
}

@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

}

 <RelativeLayout 

android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<com.google.android.gms.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="40dip" >
</com.google.android.gms.maps.MapView>

MPG
  • 785
  • 6
  • 20
0

I tried instantiating the map directly in the xml but it got me a lot of problems when trying to do more complex things with the fragments and the navigation.

The solution that gave me the best results is something like this:

1- Have an empty FrameLayout in my view's xml:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/color_separator" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:id="@+id/mapframe" />
</RelativeLayout>

2- Instantiate the SupportMapFragment in my Fragment class and add it to the FrameLayout:

if(mMapFragment==null) {
    mMapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.mapframe, mMapFragment);
    fragmentTransaction.commit();
}

3- Get the map asynchronically:

mMapFragment.getMapAsync(this);

(...)

@Override
public void onMapReady(GoogleMap googleMap) {
    //THIS IS JUST EXAMPLE CODE; PUT WHATEVER STUFF YOU NEED HERE
    mMap=googleMap;
    googleMap.addMarker(new MarkerOptions().position(endLatLng).title(getString(R.string.title)));
    mMapFragment.getView().setVisibility(View.GONE);

    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
            new LatLng(endLatLng.latitude, endLatLng.longitude), 15);
    googleMap.animateCamera(cameraUpdate);
}

This has worked perfectly for me.

jujyfruits
  • 86
  • 6