Take a close look at the layout attributes for the Button layout. I use a new attribute, style, to make this Button a small button.

Your full main.xml file should look like this:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"


<Button android:id="@+id/gpsButton" android:layout_width="fill_parent" android:layout_height="wrap_content"

android:text="Where Am I"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"


<TextView android:id="@+id/latLabel" android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="Latitude: 11

<TextView android:id="@+id/latText" android:layout_width="wrap_content"



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"


<TextView android:id="@+id/lngLabel" android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="Longitude: 11

<TextView android:id="@+id/lngText" android:layout_width="wrap_content"



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"


<view class="com.google.android.maps.MapView" android:id="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/buttonZoomIn" style="?android:attr/buttonStyleSmall" android:text="+"

android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonZoomOut" style="?android:attr/buttonStyleSmall" android:text="-"

android:layout_alignBottom="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> </LinearLayout>

You are going to make a few modifications to the code. Aside from adding the code for the new views, you need to move some existing code around. To make your Activity more flexible, you need to move the instantiations of the MapView and MapController to the main part of the class. This will allow you to then pass those items into other functions as needed (like those you will create for the zoom in and zoom out features).

final MapView myMap = (MapView) findViewById(R.id.myMap); final MapController myMapController = myMap.getController();

Now you can create the code for the two new buttons. Create the buttons as you have done in the past, adding calls to functions you will build next:

final Button zoomIn = (Button) findViewById(R.id.buttonZoomIn); zoomIn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v){


final Button zoomOut = (Button) findViewById(R.id.buttonZoomOut); zoomOut.setOnClickListener(new Button.OnClickListener() { public void onClick(View v){


Finally, create the functions that will control the zoom in and zoom out feature. The maximum zoom in level is 21 and the minimum is 1. Therefore, in your function, test for the current level before adjusting. This will ensure that you do not run into any problems.

public void ZoomIn(MapView mv, MapController mc){ if(mv.getZoomLevel()!=21){

public void ZoomOut(MapView mv, MapController mc){ if(mv.getZoomLevel()!=1){

Notice that you pass the MapView and MapController into the functions. From there, it is simply Integer manipulation to set the zoom level. The only tricky part of this function is that the MapController physically moves the MapView to the desired zoom level, whereas the MapView itself holds the zoom value.

0 0

Post a comment