SAC 2nd GIG

GPSはじめの第一歩
Android編
2009/03/28
山本 慎也
1
Ageda
▌クールなGPSアプリの紹介
▌2点間の距離や方向の計算方法
▌ソースコードの紹介
2
クールなGPSアプリの紹介(1)
▌自分の位置を取得する
▌友達の位置を取得する
▌友達との距離と方向を取得する
▌友達の方向をアニメでお知らせ
3
クールなGPSアプリの紹介(2)
自分の位置
方向をアニメでお知らせ
友達の位置、距離、方向
4
クールなGPSアプリの紹介(3)
▌とりあえず、アプリを走らせましょう。
5
2点間の距離や方向の計算(1)
▌ 2点の経緯から距離を計算しよう。
▌地球を平面として計算(近くの友達探しなら十分)
▌緯度:1度で約111km
▌経度:1度で約92km(緯度に依存)
▌X方向にdx、Y方向にdyの差を持つ2点間は...
sqrt( dx^2+ dy^2) で求まる。
6
2点間の距離や方向の計算(2)
▌ 2点の経緯から方向を計算しよう。
▌経緯からの距離の取得は前述
▌X方向にdx、Y方向にdyの差を持つ2点間の成す
角は...
atan( dy / dx ) * 180 * Πで求まるらしい。
7
サンプルコードの紹介(1)
▌自分の位置情報を取得する。
(シミュレータでのGPS情報取得~ DDMS ~ )
▌友達の位置情報を取得する。
(現在はスタブ、リアルタイムに友達の経緯を取得したい)
▌友達との距離と方向を計算する。
▌アニメで友達の方向を指し示す。
8
サンプルコードの紹介(2)
GPSアプリの名前
Project name: LocationTestApp
Package name: org.sample.locationtest
Activity name: LocationTestApp
Application name: LocationTestApp
9
自分の位置情報を取得する(1)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.sample.locationtestapp"
android:versionCode="1"
android:versionName="1.0.0">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LocationTestApp"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
AndroidManifiest.xml
10
自分の位置情報を取得する(2)
<?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"
>
<Button android:id="@+id/WhereAmI"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Where am I?"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
min.xml
典型的なボタンとテキスト
特別なことはしてません。
Check it up!
/*省略*/
<Button android:id="@+id/WhereShouldIGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Where should I go?"
/>
</LinearLayout>
11
自分の位置情報を取得する(3)
public class LocationTestApp extends Activity implements LocationListener
public void onLocationChanged(Location location) {
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
LocationTestApp.java
LocationListenerをimplements する場合上記4つのメソッドの実装が必要
12
自分の位置情報を取得する(4)
public class LocationTestApp extends Activity implements LocationListener{
/** Called when the activity is first created. */
private LocationManager LocMgr;
private String Provider;
Double MylatPoint;
Double MylngPoint;
Double direction;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationTestApp.java
LocationManagerの取得
LocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
Provider = LocMgr.getBestProvider(criteria, true);
LocMgr.requestLocationUpdates(Provider, 0, 0, this);
13
自分の位置情報を取得する(5)
public void GetMyLocation(LocationManager Mgr){
/** text 表示部分の準備 */
TextView latText = (TextView)findViewById(R.id.MylatText);
TextView lngText = (TextView)findViewById(R.id.MylngText);
Location location =
Mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if ( location != null ) {
MylatPoint = location.getLatitude();
MylngPoint = location.getLongitude();
latText.setText(MylatPoint.toString());
lngText.setText(MylngPoint.toString());
}
LocationTestApp.java
“Where am I”ボタン押下
14
GPSをシミュレートするには(1)
15
GPSをシミュレートするには(2)
←この会場の緯度経度
入力後 [Send]ボタン
16
友達の位置情報を取得する
public void GetFriendLocation(LocationManager Mgr){
/** text 表示部分の準備 */
TextView latText = (TextView)findViewById(R.id.FriendlatText);
TextView lngText = (TextView)findViewById(R.id.FriendlngText);
TextView directionText = (TextView)findViewById(R.id.DirectionText);
TextView distanceText = (TextView)findViewById(R.id.DistanceText);
//例えば、友達の居場所(緯度/経度)は、通信でリアルタイムに取得する。
//今回は、小職のオフィスの場所(福島駅周辺)
Double latPoint = 34.415040;
LocationTestApp.java
Double lngPoint = 135.292560;
/* 距離と方向の計算処理が続く */
“Where is my friend?”
ボタン押下
17
友達との距離と方向を計算する
//友達までの距離を計算しよう。
Double dx;
Double dy;
Double distance;
dx = (MylatPoint - latPoint)* 111 * 1000; //緯度1度の長さ 約111km
dy = (MylngPoint - lngPoint)* 92 * 1000; //大阪周辺の 経度1度の長さは約92km
distance = Math.sqrt(dx*dx+dy*dy);
//友達までの方向を計算しよう。北:0 西:90 南:180 東:270
direction = Math.atan2(dy,-dx) * 180 / Math.PI;
//緯度/経度/方向/距離 を出力
latText.setText(latPoint.toString());
lngText.setText(lngPoint.toString());
directionText.setText(direction.toString());
distanceText.setText(distance.toString()+"m");
}
LocationTestApp.java
“Where is my friend?”
ボタン押下(続き)
18
友達の方向をアニメでお知らせ
/** どっちにいけばいい?? */
final Button WhereShouldIGo = ( Button )findViewById(R.id.WhereShouldIGo);
WhereShouldIGo.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
DrawArrow(LocMgr);
}
LocationTestApp.java
});
“Where should I go?”
ボタン押下
}
public void DrawArrow(LocationManager Mgr){
Intent myIntent = new Intent(this, OpenGL.class);
myIntent.putExtra("DIRECTION", direction);
startActivity(myIntent);
}
指の矢印は3Dグラフィックスで表現しています。
ご興味ある方はOpenGLクラスを追ってみて下さい。
19
ご質問はありますか?
▌ ご清聴ありがとうございました。
20