Rhisl Toggling Between Map Views Standard and Satellite Views

Edit the AndroidLBS Activity one more time. You should add two more buttons to the RelativeLayout. These buttons should toggle the MapView between standard view and satellite view. Here are some points to consider:

• Add the toggle buttons to the opposite corners of the MapView using the align layout attributes.

• Research the MapView to find the toggling method.

• Create a function that you can pass the MapView to and toggle it.

The complete text of solution main.xml and AndroidLBS.java are as follows.

main.xml

<?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"

android:layout_height="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"

android:layout_height="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"

android:layout_height="wrap_content"

</LinearLayout>

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

android:layout_height="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"

android:layout_height="wrap_content"

</LinearLayout>

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

android:layout_height="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/buttonMapView" style="?android:attr/buttonStyleSmall" android:text="Map"

android:layout_alignRight="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonSatView" style="?android:attr/buttonStyleSmall" android:text="Sat"

android:layout_alignRight="@+id/myMap" android:layout_alignBottom="@+id/myMap" 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>

AndroidLBS.java package android_programmers_guide.AndroidLBS;

import android.os.Bundle;

import android.location.LocationManager;

import android.view.View;

import android.widget.TextView;

import android.content.Context;

import android.widget.Button;

import com.google.android.maps.MapActivity;

import com.google.android.maps.MapView;

import com.google.android.maps.Point;

import com.google.android.maps.MapController;

public class AndroidLBS extends MapActivity {

/** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

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

ZoomIn(myMap,myMapController);

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

ZoomOut(myMap,myMapController);

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

LoadProviders(myMap,myMapController);

final Button viewMap = (Button) findViewById(R.id.buttonMapView); viewMap.setOnClickListener(new Button.OnClickListener() { public void onClick(View v){ ShowMap(myMap);

final Button viewSat = (Button) findViewById(R.id.buttonSatView); viewSat.setOnClickListener(new Button.OnClickListener() { public void onClick(View v){ ShowSat(myMap);

public void LoadProviders(MapView mv, MapController mc){ TextView latText = (TextView) findViewById(R.id.latText); TextView lngText = (TextView) findViewById(R.id.lngText); LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Double latPoint = myManager.getCurrentLocation("gps").getLatitude()*1E6;

Double lngPoint = myManager.getCurrentLocation("gps").getLongitude()*1E6; latText.setText(latPoint.toString()); lngText.setText(lngPoint.toString());

Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue());

mc.centerMapTo(myLocation, false);

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

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

public void ShowMap(MapView mv){ if (mv.isSatellite()){

mv.toggleSatellite();

public void ShowSat(MapView mv){ if (!mv.isSatellite()){

mv.toggleSatellite();

When you run your Activity, you should be able to toggle the satellite view on and off, as shown in the following illustrations.

In the next chapter you will dive deeper into the Google API. Chapter 10 will walk you step by step through the process of using the Google API to send GTalk messages to and from an Android phone.

0 0

Post a comment