I'm trying to get the zoom controls to show up in a mapview
, the following code almost works, but the zoom controls appear in the top left of the mapview
, not the bottom center like I'm specifying via setGravity()
. Can someone enlighten me as to what I'm missing?
zoomView = (LinearLayout) mapView.getZoomControls();
zoomView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
zoomView.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
mapView.addView(zoomView);
These views/layouts are all constructed programmatically, there is no layout file to tweak.
In Android, Zoom Control is a class that has some set of methods that are used to control the zoom functionality. It has two buttons that are used to control the zoom functionality (ie Zoom In and Zoom Out).
Want to add zoom controls to Android MapView? We can achieve this by calling setBuiltInZoomControls(boolean) method. map. setBuiltInZoomControls(true);
Add the following line to the OnCreate()
method of your MapView
Class:
view.setBuiltInZoomControls(true);
The trick here is to place another Layout container where you want to put the ZoomControls and then insert the ZoomControls into that.
The real trick is to use the RelativeLayout
rather than LinearLayout
to position the elements, as shown in this sample layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="MY_MAP_API_KEY"
/>
<LinearLayout android:id="@+id/layout_zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
The layout_zoom LinearLayout element is positioned in the bottom center of the screen, placing it over the middle/bottom of the MapView
.
Then within your Activity's onCreate
, get a reference to the layout_zoom element and insert the ZoomControl into it, much like you've already done:
LinearLayout zoomLayout =(LinearLayout)findViewById(R.id.layout_zoom);
View zoomView = myMapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
myMapView.displayZoomControls(true);
The ZoomControls should now appear on a long click, without stealing the map touch events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With