Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, How to add Google map options from XML layout?

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

like image 521
Hesam Avatar asked Apr 24 '13 03:04

Hesam


1 Answers

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.

like image 68
Hesam Avatar answered Nov 09 '22 17:11

Hesam