Android SDK1.5でZoomControlsが使えない件
前回作成したGoogleMapにズーム機能を追加します。
勉強会/GoogleMap - 日本Androidの会(日本アンドロイドの会)
を参考にしましたが、上記サイトではSDK1.1で作成されています。
これをSDK1.5で作成します。
ZoomControlsを使った失敗例
main.xmlの編集
- MapViewにidの追加
- clickableの追加
- ZoomControlsを配置するためLinearLayoutの追加
main.xml
<com.google.android.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="API KEY" /> <LinearLayout android:id="@+id/zoomview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/mapview" android:layout_centerHorizontal="true" />
SampleGoogleMap.javaの編集
勉強会/GoogleMap - 日本Androidの会(日本アンドロイドの会)
を参考に同じものを作成
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearLayout;
MapView mapView;
ZoomControls mZoom;
linearLayout = (LinearLayout)findViewById(R.id.zoomview);
mapView = (MapView) findViewById(R.id.mapview);
mZoom = (ZoomControls) mapView.getZoomControls();
linearLayout.addView(mZoom);
ところが以下の状態が発生!
「getZoomControls」と表示され、
The method getZoomControls() from the type MapView is deprecated
と怒られる
Warningレベルなので、コンパイル、実行はOKだが、肝心のズーム機能は動作しない
エラー起きないように修正
以下のように修正すると正しく動作する
- mapView.setBuiltInZoomControls(true);を追加
- getZoomControlsは使う必要がないのでごっそり削除
- main.xmlのLinearLayoutも不要なので削除
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" > <com.google.android.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="API KEY" />
SampleGoogleMap.java
package sample.googlemap; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.ZoomControls; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; public class SampleGoogleMap extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView; mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
実行結果
画面をタップするとズームコントローラが表示される
「+」「-」をクリックして縮尺が変更可能になる。
参考サイト
勉強会/GoogleMap - 日本Androidの会(日本アンドロイドの会)
http://magpad.jugem.jp/?eid=46