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

I have a fragment containing a MapView. I added this view to the XML file as follows:

<?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 linked it with my code as follows:

 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 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 was mentioned in GoogleMapOptions , "These parameters can be used when adding a map to your application programmatically (unlike XML). If you use MapFragment, you can pass these parameters using the static factory newInstance (GoogleMapOptions) method. If you use MapView, you can pass these parameters using the MapView constructor (Context, GoogleMapOptions). " and finally my case, " If you add a map using XML, you can apply these parameters using custom XML tags. "

I have not found a single example to show how to add parameters via XML. I want to add zOrderOnTop = "true" to my XML code.

Any suggestion will be appreciated. Thanks

+4
source share
1 answer

It was as simple 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, a new problem is adding map:zOrderOnTop="true" will remove overlay objects, such as ZoomIn / ZoomOut, from the screen :( For more information, see this link .

+4
source

All Articles