Thursday, November 12, 2009

setBuiltInZoomControls on MapView

If you read the article Hello, MapView from Android, ZoomControls widget is suggested for zooming in and out of a View. MapView can automatically hook one for us by requesting it with the getZoomControls() method. But...actually it is now deprecated.

Instead of ZoomControls, MapView.setBuiltInZoomControls(true) can be used to add zoom mechanism on MapView.



Modify from previous article, AndroidLocation: with a CheckBox to toggle MapView.setSatellite().

Modify main.xml to remove SeekBar from the previous layout, and change android:clickable of com.google.android.maps.MapView to "true".

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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="@+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Satellite "
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude:"
/>
</LinearLayout>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="-----Your Own API Key here-------------"
/>
</LinearLayout>


Modify AndroidLocation.java, remove all myZoomBar related code, and add the code
myMapView.setBuiltInZoomControls(true);

package com.AndroidLocation;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidLocation extends MapActivity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLongitude, myLatitude;
private CheckBox mySatellite;

private MapView myMapView;

private MapController myMapController;

private void CenterLocation(GeoPoint centerGeoPoint)
{
myMapController.animateTo(centerGeoPoint);


myLongitude.setText("Longitude: "+
String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
);
myLatitude.setText("Latitude: "+
String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
);
};

private void SetSatellite()
{
myMapView.setSatellite(mySatellite.isChecked());
};

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

myMapView = (MapView)findViewById(R.id.mapview);

myMapView.setBuiltInZoomControls(true);


myLongitude = (TextView)findViewById(R.id.longitude);
myLatitude = (TextView)findViewById(R.id.latitude);
mySatellite = (CheckBox)findViewById(R.id.satellite);

SetSatellite();
myMapController = myMapView.getController();

myLocationManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);

myLocationListener = new MyLocationListener();

myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
myLocationListener);

//Get the current location in start-up
//check LastKnownLocation, if not valid, skip it.
Location initLocation=myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(initLocation != null)
{
GeoPoint initGeoPoint = new GeoPoint(
(int)(initLocation.getLatitude()*1000000),
(int)(initLocation.getLongitude()*1000000));
CenterLocation(initGeoPoint);
}
mySatellite.setOnClickListener(mySatelliteOnClickListener);
}

private CheckBox.OnClickListener mySatelliteOnClickListener =
new CheckBox.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
SetSatellite();
}

};


private class MyLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {
// TODO Auto-generated method stub
GeoPoint myGeoPoint = new GeoPoint(
(int)(argLocation.getLatitude()*1000000),
(int)(argLocation.getLongitude()*1000000));

CenterLocation(myGeoPoint);
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
};
}


Download the files.

No comments: