- Home ›
- Android入門 ›
- MapViewクラス ›
- HERE
地図の縮尺を設定
広告
地図の縮尺を設定する方法を確認します。「MapController」クラスで用意されている「zoomTo」メソッドを使います。
zoomTo public void zoomTo(int zoomLevel)
Sets the zoomlevel of the map. The value will be clamped to be between
1 and 21 inclusive, though not all areas have tiles at higher zoom levels.
Parameters:
zoomLevel At zoomLevel 1, the equator of the earth is 256 pixels long.
Each successive zoom level is magnified by a factor of 2.
引数に地図に設定したい縮尺レベルをint型の値で指定します。指定できる値は現時点では1から21までの間で、場所によってどこまでの縮尺レベルの地図が用意されているはかは異なります。
具体的には次のように記述します。
@Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
MapView map = new MapView(this);
MapController mc = map.getController();
Point point = new Point(35690921, 139700258);
mc.centerMapTo(point, true);
mc.zoomTo(12);
}
サンプルプログラム
それでは実際に試してみます。プロジェクトを作成しソースコードを次のように変更しました。
package jp.javadrive.android;
import com.google.android.maps.MapActivity;
import android.os.Bundle;
import com.google.android.maps.MapView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Color;
import android.view.View.OnClickListener;
import com.google.android.maps.Point;
import com.google.android.maps.MapController;
public class Test09_01 extends MapActivity
implements OnClickListener{
private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private MapView map;
private Button buttonZoomin;
private Button buttonZoomout;
private MapController mc;
private TextView textZoomlevel;
@Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
LinearLayout btnLinearLayout = new LinearLayout(this);
btnLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
btnLinearLayout.setBackgroundColor(Color.BLACK);
buttonZoomin = new Button(this);
buttonZoomin.setText("+");
buttonZoomin.setOnClickListener(this);
buttonZoomout = new Button(this);
buttonZoomout.setText("-");
buttonZoomout.setOnClickListener(this);
textZoomlevel = new TextView(this);
textZoomlevel.setText("Level:15");
textZoomlevel.setTextColor(Color.WHITE);
btnLinearLayout.addView(buttonZoomin, createParam(WC, WC));
btnLinearLayout.addView(buttonZoomout, createParam(WC, WC));
btnLinearLayout.addView(textZoomlevel, createParam(WC, WC));
map = new MapView(this);
mc = map.getController();
Point p = createPoint(35.690921f, 139.700258f);
mc.centerMapTo(p, false);
mc.zoomTo(15);
linearLayout.addView(btnLinearLayout, createParam(FP, WC));
linearLayout.addView(map, createParam(WC, WC));
}
private LinearLayout.LayoutParams createParam(int w, int h){
return new LinearLayout.LayoutParams(w, h);
}
public void onClick(View v) {
if (v == buttonZoomin){
int level = map.getZoomLevel();
mc.zoomTo(level + 1);
textZoomlevel.setText("Level:" + map.getZoomLevel());
}else if (v == buttonZoomout){
int level = map.getZoomLevel();
mc.zoomTo(level - 1);
textZoomlevel.setText("Level:" + map.getZoomLevel());
}
}
private Point createPoint(float lat, float lng){
Point p = new Point((int)(lat * 1000000), (int)(lng * 1000000));
return p;
}
}
ビルド後にエミュレーター上で実行します。
「+」と書かれたボタンをクリックするとズームインし、「-」と書かれたボタンをクリックするとズームアウトします。
なお、この地点における最大ズームレベルは「17」で最小ズームレベルは「1」でした。
( Written by Tatsuo Ikura )
JavaDrive