会社と家とやってることに差がないw
さて一度はやってみたい自分の位置を地図上に描画。
今日一日の作業メモを複数回に分けて記録。
動作確認環境
- OS:Windows7 64bit上でのエミュレータ
- HT-03Aでも一応動いたことは確認
- Google API (APILevel4以上)が使用できること
- GPS有効に
Android上に地図を表示するためには、MapActivityを継承したクラスとレイアウトにMapViewを使用すればOK!
素敵です。
そして、そのMapView上にOverlayクラスを使えば地図上に描画もできる。
あとは、LocationListenerを使用してGPSを取得して・・・。
っていうかそのOverlayにLocationListener+SensorListenerをimplimentしたクラスMyLocationOverlayってのが用意されています。
なので以下の実装だけでとりあえず現在位置は描画できちゃいました。
layout/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/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="取得したAPIキーを入れる" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" //中略 <uses-library android:name="com.google.android.maps"></uses-library> </application> <uses-sdk android:minSdkVersion="4" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>MapViewとか使うので
・<uses-library android:name="com.google.android.maps"></uses-library>
の追加を忘れずに
さらにネットに繋ぐ前提となるので
・<uses-permission android:name="android.permission.INTERNET"></uses-permission>
を追加
もちろんGPS使うので
・<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
も追加
main.java
package com.omkageru.ak.gpschk;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.os.Bundle;
public class Main extends MapActivity {
private MapView mapView;
private MyLocationOverlay overlay;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.map);
mapView.setEnabled(true);
// ここから下はお好みで
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
// 今回の主役。有効にすることでGPSの取得が可能に
overlay = new MyLocationOverlay(getApplicationContext(), mapView);
overlay.enableMyLocation();
// GPS取得が可能な状態になり、GPS初取得時の動作を決定(らしい)
overlay.runOnFirstFix(new Runnable(){
public void run() {
// TODO 自動生成されたメソッド・スタブ
// animateTo(GeoPoint)で指定GeoPoint位置に移動
// この場合、画面中央がGPS取得による現在位置になる
mapView.getController().animateTo(overlay.getMyLocation());
}
});
// Overlayとして登録
mapView.getOverlays().add(overlay);
// とりあえず再描画
mapView.invalidate();
}
/* (非 Javadoc)
* @see com.google.android.maps.MapActivity#onDestroy()
*/
@Override
protected void onDestroy() {
// 破棄されるときには無効にしておく
overlay.disableMyLocation();
mapView.getOverlays().remove(overlay);
// TODO 自動生成されたメソッド・スタブ
super.onDestroy();
}
@Override
protected boolean isRouteDisplayed() {
// TODO 自動生成されたメソッド・スタブ
return false;
}
}
とまあこんな感じですか?
一応動くはずです。
あ、基本Cの人で、Javaは勘とeclipse先生にお任せしているのでお作法がおかしいとかありましたら、やさしく教えてください。
動かすと以下みたいな感じに。(エミュレータ画面)
起動したて
GPS情報を流し込む
Google本社の場所が!
緯度38 経度138 とかそんな値を突っ込んだ結果
なお現状の実装では画面をタップして適当な箇所に動かした後、GPS情報を流し込んでもその場所へは移動しません。
参考 「Android プログラミング入門」
・第四部 5.2.3 MyLocationOverlay





0 件のコメント:
コメントを投稿