I've been trying my best to work around this issue for a few days and I've read suggestions from other StackOverFlow posts in addition to plenty of Google searches. I think the main issue is that the 'getMap()' method has been deprecated to make way for 'getMapAsync()' and the explanations for using the latter don't fit with how I want to use it. So, with this in mind, I figured a question might be relevant to others as well at this point.
Basically, so far I've created a menu out of buttons that allow the user to switch between Fragments. One of these Fragments needs to be a GoogleMap which will start on a button press, keeping the buttons in view.
I got my MapsActivity code from: -http://www.joellipman.com/articles/google/android/application-development/android-os-add-googlemap-as-fragment.html - but as mentioned, getMap() is deprecated. I also looked at -https://stackoverflow.com/questions/19353255/how-to-put-google-maps-v2-on-a-fragment-using-viewpager - but the problem is the same.
MainActivity...
public class MainActivity extends AppCompatActivity {
Button mapMenuItem, postcodeMenuItem, nameMenuItem, recentMenuItem;
ConnectivityManager conMgr;
NetworkInfo networkInfo;
FrameLayout fragmentContainer;
NameFragment nf;
String longiString, latiString;
public static FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapMenuItem = (Button) findViewById(R.id.mapButton);
postcodeMenuItem = (Button) findViewById(R.id.postcodeButton);
nameMenuItem = (Button) findViewById(R.id.nameButton);
recentMenuItem = (Button) findViewById(R.id.recentButton);
fragmentContainer = (FrameLayout) findViewById(R.id.fragmentContainer);
fragmentManager = getFragmentManager();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
public void searchMap(View v) {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
MapFragment mf = new MapFragment();
ft.replace(R.id.fragmentContainer, mf);
ft.commit();
mapMenuItem.setBackgroundColor(Color.parseColor("#fece35"));
nameMenuItem.setBackgroundColor(Color.parseColor("#eff169"));
postcodeMenuItem.setBackgroundColor(Color.parseColor("#eff169"));
recentMenuItem.setBackgroundColor(Color.parseColor("#eff169"));
}
}
MapFragment...
public class MapFragment extends Fragment {
MapView mapView;
GoogleMap gMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_fragment, container, false);
mapView = (MapView) v.findViewById((R.id.mapView));
mapView.onCreate(savedInstanceState);
mapView.onResume();
try{
MapsInitializer.initialize(getActivity().getApplicationContext());
}catch (Exception e){
e.printStackTrace();
}
gMap = mapView.getMap();
double latitude = 17.385044;
double longitude = 78.486671;
//create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
//changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
//adding marker
gMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(17.385044, 78.486671)).zoom(12).build();
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//Perform any camera updates
return v;
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
MapLayout...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="@string/google_maps_key"
android:clickable="false"/>
</LinearLayout>
ActivityLayout...
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#72a972"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="uk.ac.mmu.webmd.foodhygieneapp.MainActivity">
<RelativeLayout
android:id="@+id/rel_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#eff169"
android:gravity="center_horizontal">
<Button
android:id="@+id/mapButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#eff169"
android:onClick="searchMap"
android:text="@string/MapsButton" />
<Button
android:id="@+id/nameButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/mapButton"
android:background="#eff169"
android:onClick="nameMenu"
android:text="@string/NameButton" />
<Button
android:id="@+id/postcodeButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/recentButton"
android:background="#eff169"
android:onClick="postcodeMenu"
android:text="@string/PostcodeButton" />
<Button
android:id="@+id/recentButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:background="#eff169"
android:onClick="recentMenu"
android:text="@string/RecentButton" />
</RelativeLayout>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>