2008年2月25日 星期一

MapActivity in Android 2

You can control google map further. 你可以更進一步地使用Google map。

1. mapdemo.java:再將上一篇的程式碼加上籃色部份,可用手機按鍵控制地圖,O為拉遠、I為拉近、S為衛星地圖、上下左右鍵為移動。


package com.test.demo;

import com.google.android.maps.MapActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Point;

public class mapdemo extends MapActivity{
/** Called when the activity is first created. */
public static final int change = 3000;
private MapView mapView;
private MapController mapConrtoller;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

//get id from main.xml
mapView =(MapView) findViewById(R.id.my_map);
mapConrtoller = mapView.getController();
mapConrtoller.zoomTo(17); updateView();
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_I) {
// zoom in
mapView.getController().zoomTo(mapView.getZoomLevel() + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_O) {
// zoom out
mapView.getController().zoomTo(mapView.getZoomLevel() - 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_S) {
// 衛星地图
if(!mapView.isSatellite()){
mapView.toggleSatellite(); }
return true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
Double lat = mapView.getMapCenter().getLatitudeE6()/1.0;
Double lng = mapView.getMapCenter().getLongitudeE6()/1.0;
lat=lat+change;
Point point = new Point(lat.intValue(),lng.intValue());
mapConrtoller.animateTo(point); return true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
Double lat = mapView.getMapCenter().getLatitudeE6()/1.0;
Double lng = mapView.getMapCenter().getLongitudeE6()/1.0;
lat=lat-change;
Point point = new Point(lat.intValue(),lng.intValue());
mapConrtoller.animateTo(point); return true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
Double lat = mapView.getMapCenter().getLatitudeE6()/1.0;
Double lng = mapView.getMapCenter().getLongitudeE6()/1.0;
lng=lng-change;
Point point = new Point(lat.intValue(),lng.intValue());
mapConrtoller.animateTo(point); return true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
Double lat = mapView.getMapCenter().getLatitudeE6()/1.0;
Double lng = mapView.getMapCenter().getLongitudeE6()/1.0;
lng=lng+change;
Point point = new Point(lat.intValue(),lng.intValue());
mapConrtoller.animateTo(point); return true;
}
return false;
}
private void updateView(){
Double lat = 25.025708*1E6;
Double lng = 121.526403*1E6;
Point point = new Point(lat.intValue(), lng.intValue());
mapConrtoller.centerMapTo(point, false); }

}


沒有留言: