ワーキンググループ/勉強会?
GPSから現在位置を取得するAPIを用いて、ロケーションベースサービスの基礎となるアプリケーションを作成してみましょう。
アンドロイドでは、携帯電話の現在位置を取得するサービスを、位置情報サービス(LBS: Location-Based Services)と呼んでいます。LBSは、通常は人工衛星の配置を利用したGPS(Global Positioning System)を利用して位置情報を取得します。しかし、無線LANのアクセスポイントを解析して位置を特定するなど、今後新たに登場する位置情報取得サービスを考慮して、GPSという名前ではなく、より一般化されたLBSという名前を使用しています。
以下、作成のポイントです。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.hews.hellogps" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloGpsActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="com.google.android.maps"/> </application> <uses-permission android:name="android.permission.INTERNET"/> <!-- GPSを使用するために必要なパーミッション --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> </manifest>
<?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:enabled="true" android:clickable="true" android:apiKey="<取得したAPIキー>" /> </LinearLayout>
現在位置の変化を受け取るために、LocationListnerを実装します。また、現在位置を表示するために、MapActivityを継承します。
public class HelloGpsActivity extends MapActivity implements LocationListener {
protected boolean isRouteDisplayed() {} public void onLocationChanged(Location location) {} public void onProviderDisabled(String provider) {} public void onProviderEnabled(String provider) {} public void onStatusChanged(String provider, int status, Bundle extras) {}
LocationManager l = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
l.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
public class HelloGpsActivity extends MapActivity implements LocationListener { // フィールドを追加 MapController m_controller; public void onCreate(Bundle savedInstanceState) { ...; // 解説はHelloMapを参照 MapView m = (MapView)findViewById(R.id.mapview); m_controller = m.getController(); m_controller.setZoom(15); ZoomControls zc = (ZoomControls) m.getZoomControls(); zc.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); zc.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL); m.addView(zc); }
public void onLocationChanged(Location location) { GeoPoint gp = new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)); m_controller.animateTo(gp); }
package jp.hews.hellogps; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.Gravity; import android.view.ViewGroup; import android.widget.ZoomControls; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; public class HelloGpsActivity extends MapActivity implements LocationListener { // フィールドを追加 MapController m_controller; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 現在位置が変化に、メソッドが呼び出されよう登録する。 LocationManager l = (LocationManager)getSystemService(Context.LOCATION_SERVICE); l.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); // 解説はHelloMapを参照 MapView m = (MapView)findViewById(R.id.mapview); m_controller = m.getController(); m_controller.setZoom(15); ZoomControls zc = (ZoomControls) m.getZoomControls(); zc.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); zc.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL); m.addView(zc); } @Override protected boolean isRouteDisplayed() { return false; } public void onLocationChanged(Location location) { GeoPoint gp = new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)); m_controller.animateTo(gp); } public void onProviderDisabled(String provider) { // TODO } public void onProviderEnabled(String provider) { // TODO } public void onStatusChanged(String provider, int status, Bundle extras) { // TODO } }