Presentation is loading. Please wait.

Presentation is loading. Please wait.

GPSはじめの第一歩 Android編 2009/03/28 山本 慎也.

Similar presentations


Presentation on theme: "GPSはじめの第一歩 Android編 2009/03/28 山本 慎也."— Presentation transcript:

1 GPSはじめの第一歩 Android編 2009/03/28 山本 慎也

2 Ageda クールなGPSアプリの紹介 2点間の距離や方向の計算方法 ソースコードの紹介

3 クールなGPSアプリの紹介(1) 自分の位置を取得する 友達の位置を取得する 友達との距離と方向を取得する 友達の方向をアニメでお知らせ

4 クールなGPSアプリの紹介(2) 自分の位置 方向をアニメでお知らせ 友達の位置、距離、方向

5 クールなGPSアプリの紹介(3) とりあえず、アプリを走らせましょう。

6 2点間の距離や方向の計算(1) 2点の経緯から距離を計算しよう。 地球を平面として計算(近くの友達探しなら十分) 緯度:1度で約111km
経度:1度で約92km(緯度に依存) X方向にdx、Y方向にdyの差を持つ2点間は... sqrt( dx^2+ dy^2) で求まる。

7 2点間の距離や方向の計算(2) 2点の経緯から方向を計算しよう。 経緯からの距離の取得は前述
X方向にdx、Y方向にdyの差を持つ2点間の成す角は... atan( dy / dx ) * 180 * Πで求まるらしい。

8 サンプルコードの紹介(1) 自分の位置情報を取得する。 友達の位置情報を取得する。 友達との距離と方向を計算する。
(シミュレータでのGPS情報取得~ DDMS ~ ) 友達の位置情報を取得する。 (現在はスタブ、リアルタイムに友達の経緯を取得したい) 友達との距離と方向を計算する。 アニメで友達の方向を指し示す。

9 サンプルコードの紹介(2) GPSアプリの名前 Project name: LocationTestApp
Package name: org.sample.locationtest Activity name: LocationTestApp Application name: LocationTestApp

10 自分の位置情報を取得する(1) AndroidManifiest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns: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 <activity android:name=".LocationTestApp" <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> AndroidManifiest.xml

11 自分の位置情報を取得する(2) min.xml 典型的なボタンとテキスト 特別なことはしてません。 Check it up!
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_height="wrap_content" android:text="Where am I?" /> android:layout_width="wrap_content" /*省略*/ <Button android:text="Where should I go?" </LinearLayout> min.xml 典型的なボタンとテキスト 特別なことはしてません。 Check it up!

12 LocationListenerをimplements する場合上記4つのメソッドの実装が必要
自分の位置情報を取得する(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つのメソッドの実装が必要

13 自分の位置情報を取得する(4) LocationTestApp.java LocationManagerの取得
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); LocMgr = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); Provider = LocMgr.getBestProvider(criteria, true); LocMgr.requestLocationUpdates(Provider, 0, 0, this); LocationTestApp.java LocationManagerの取得

14 自分の位置情報を取得する(5) LocationTestApp.java “Where am I”ボタン押下
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”ボタン押下

15 GPSをシミュレートするには(1)

16 GPSをシミュレートするには(2) ←この会場の緯度経度 入力後 [Send]ボタン

17 友達の位置情報を取得する LocationTestApp.java “Where is my friend?” ボタン押下
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 = ; Double lngPoint = ; /* 距離と方向の計算処理が続く */ LocationTestApp.java “Where is my friend?” ボタン押下

18 友達との距離と方向を計算する LocationTestApp.java “Where is my friend?” ボタン押下(続き)
//友達までの距離を計算しよう。 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?” ボタン押下(続き)

19 友達の方向をアニメでお知らせ LocationTestApp.java “Where should I go?” ボタン押下
/** どっちにいけばいい?? */ final Button WhereShouldIGo = ( Button )findViewById(R.id.WhereShouldIGo); WhereShouldIGo.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ DrawArrow(LocMgr); } }); public void DrawArrow(LocationManager Mgr){ Intent myIntent = new Intent(this, OpenGL.class); myIntent.putExtra("DIRECTION", direction); startActivity(myIntent); LocationTestApp.java “Where should I go?” ボタン押下 指の矢印は3Dグラフィックスで表現しています。 ご興味ある方はOpenGLクラスを追ってみて下さい。

20 ご質問はありますか? ご清聴ありがとうございました。


Download ppt "GPSはじめの第一歩 Android編 2009/03/28 山本 慎也."

Similar presentations


Ads by Google