5

I have a fragment that holds a MapView. I have added this view in XML file like this:

<?xml version="1.0" encoding="utf-8"?>

<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    map:uiCompass="true"
    android:background="#00000000" />

And I've linked it to my code like this:

public class HotSpotsFragment extends MainFragment implements LocationListener {

    private static final String TAG = "HotSpotsFragment";
    private Context context;
    private LocationManager locationManager;
    private MapView mapView;
    private GoogleMap googleMap;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // create view
        View view = inflater.inflate(R.layout.fragment_hot_spots, container, false);

        // Getting context
        context = getActivity().getApplicationContext();

        // Make sure user's device supports Google play services
        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            Log.e(TAG, "Google play service is not available.");
            Toast.makeText(getActivity().getApplicationContext(), R.string.hot_spots_not_supported, Toast.LENGTH_LONG).show();
            return null;
        }

        Log.i(TAG, "Google play service is available.");

        mapView = (MapView) view.findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);

        googleMap = ((MapView) view.findViewById(R.id.mapView)).getMap();
        if(googleMap == null) {
            Toast.makeText(getActivity().getApplicationContext(), R.string.hot_spots_not_supported, Toast.LENGTH_LONG).show();
            return null;
        }

        Log.i(TAG, "View created");

        return view;
    }
.
.
.
}

I want to add to add options to my map view. Based on what has been mentioned on GoogleMapOptions, "These options can be used when adding a map to your application programmatically (as opposed to via XML). If you are using a MapFragment, you can pass these options in using the static factory method newInstance(GoogleMapOptions). If you are using a MapView, you can pass these options in using the constructor MapView(Context, GoogleMapOptions)." and finally my case,"If you add a map using XML, then you can apply these options using custom XML tags."

I didn't find any sample to show how to add options through XML. I want to add zOrderOnTop="true" into my XML code.

Any suggestion would be appreciated. Thanks

Charles
  • 50,943
  • 13
  • 104
  • 142
Hesam
  • 52,260
  • 74
  • 224
  • 365
  • Hi Hesam! I am not able to display MapView if i use the below code **mapView = (MapView) view.findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); googleMap = ((MapView) view.findViewById(R.id.mapView)).getMap();** – KK_07k11A0585 Jul 25 '13 at 07:16
  • But, when i specify **mapView.onResume();** after **mapView.onCreate(savedInstance)** it is working What am i doing wrong? Is it possible to display map without specifying **onResume()** ? Plz help me – KK_07k11A0585 Jul 25 '13 at 07:20
  • 1
    I guess you have not implemented other required method. That's why it happens. You no need to write "mapView.onResume();" in onCreateView() method. Make sure you have following methods after onCreateView(): – Hesam Jul 25 '13 at 16:43
  • 1
    *Override public void onResume() { super.onResume(); if(mapView != null) mapView.onResume(); } *Override public void onPause() { super.onPause(); if(mapView != null) mapView.onPause(); } *Override public void onDestroy() { super.onDestroy(); if(mapView != null) mapView.onDestroy(); } – Hesam Jul 25 '13 at 16:44
  • 1
    *Override public void onSaveInstanceState(Bundle outState) { if(mapView != null) mapView.onSaveInstanceState(outState); super.onSaveInstanceState(outState); } *Override public void onLowMemory() { if(mapView != null) mapView.onLowMemory(); super.onLowMemory(); } – Hesam Jul 25 '13 at 16:44
  • 1
    please replace * with @. I couldn't user that because editor said not more than 1 @ is allowed :( – Hesam Jul 25 '13 at 16:46
  • Hi Hesam Thank's a lot for your answer. I was able to display map. But when i navigate to another activity/fragment and come back then it is not saving the previous state of map. The map is being reloaded again. Plz help me ... i dont want the map to reload again ... – KK_07k11A0585 Jul 26 '13 at 13:48
  • 1
    Please look at http://stackoverflow.com/a/14114428/513413 I think it solves your problem. – Hesam Jul 27 '13 at 07:16

1 Answers1

6

It was as easy as

<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    map:uiCompass="true"
    map:zOrderOnTop="true"
    map:uiZoomControls="true"
    android:background="#00000000" />

However new problem is adding map:zOrderOnTop="true" will remove overlay objects such as ZoomIn/ZoomOut from screen :( For more info refer to this link.

Hesam
  • 52,260
  • 74
  • 224
  • 365