簡易ルート検索アプリを作る
以下は、今回作成する簡易ルート検索アプリのソースコードの全容となります。
<あなたのアプリケーションID>の部分はご自分で取得されたアプリケーションIDに書き換えてください。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.android.app.my.RouteSearch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask"
android:label="@string/app_name"
android:name=".RouteSearchActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" ></uses-permission>
<!-- MapViewを使用するのに必要な設定 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" android:resizeable="true"></supports-screens>
</manifest>
package jp.android.app.my.RouteSearch;
import jp.co.yahoo.android.maps.GeoPoint;
import jp.co.yahoo.android.maps.MapActivity;
import jp.co.yahoo.android.maps.MapController;
import jp.co.yahoo.android.maps.MapView;
import jp.co.yahoo.android.maps.PinOverlay;
import jp.co.yahoo.android.maps.routing.RouteOverlay;
import jp.co.yahoo.android.maps.routing.RouteOverlay.RouteOverlayListener;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.location.*;
import android.os.Bundle;
import android.view.*;
import android.view.ViewGroup.*;
import android.widget.*;
public class RouteSearchActivity extends MapActivity implements LocationListener, RouteOverlayListener, MapView.MapTouchListener{
private MapView mMapView=null;//Y地図View
private PinOverlay mPinOverlay=null;//開始位置のピン
private RouteOverlay mRouteOverlay=null;//ルート検索Overlay
private GeoPoint mStartPos;//出発地(緯度経度)
private GeoPoint mGoalPos;//目的地(緯度経度)
private ProgressDialog mProgDialog = null;//プログレスダイアログ
private TextView mDistLabel=null;//距離表示用テキストビュー
private LocationManager mLocationManager = null;//ロケーションマネージャー
private static final int MENUITEM_CLEAR = 1;//メニュー(クリア)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//現在位置 (NETWORK)
mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,0,this);
//プログレスダイアログを表示
mProgDialog = new ProgressDialog(this);
mProgDialog.setMessage("現在位置取得中");
mProgDialog.show();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//クリアメニューを追加
menu.removeItem(MENUITEM_CLEAR);
menu.add(0, MENUITEM_CLEAR, 0, "クリア");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//メニュー選択処理
switch (item.getItemId()) {
case MENUITEM_CLEAR:
//地図上からルートと距離表示をクリア
if(mMapView!=null){
mMapView.removeOverlay(mRouteOverlay);
mRouteOverlay = null;
if(mDistLabel!=null) mDistLabel.setVisibility(View.INVISIBLE);
}
return true;
}
return false;
}
//ロケーション情報取得
public void onLocationChanged(Location location) {
//プログレスダイアログを停止
if(mProgDialog!=null){
mProgDialog.dismiss();
mProgDialog = null;
}
//LocationManagerを停止
mLocationManager.removeUpdates(this);
//MapViewを作成
mMapView = new MapView(this,"<あなたのアプリケーションID>");
//地図の表示位置をLocationManagerで取得した位置に変更
MapController mapController = mMapView.getMapController();
GeoPoint centerPos = new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
mapController.setCenter(centerPos);
//ズームレベルをセット
mapController.setZoom(4);
//地図上で長押しイベントの発行を許可する
mMapView.setLongPress(true);
//MapTouchListenerを設定
mMapView.setMapTouchListener(this);
//LocationManagerで取得した位置にピンを立てる
mPinOverlay = new PinOverlay(PinOverlay.PIN_VIOLET);
mMapView.getOverlays().add(mPinOverlay);
mPinOverlay.addPoint(centerPos,null);
//LocationManagerで取得した位置をルート開始位置とします
mStartPos = centerPos;
//MapViewをカレントビューに追加
setContentView(mMapView);
}
//MapView.MapTouchListener::onLongPress 地図の長押しイベント
public boolean onLongPress(MapView map, Object obj, PinOverlay pin,GeoPoint point) {
//前回の処理を停止
if(mRouteOverlay!=null) mRouteOverlay.cancel();
//目的地を設定
mGoalPos = point;
//長押しピンをクリア onLongPressが発生すると自動的にピンが追加されるので、ここで削除しておきます。
mMapView.removeOverlay(pin);
//前回のRouteOverlayを地図から削除
mMapView.removeOverlay(mRouteOverlay);
//距離テキストビューを非表示
if(mDistLabel!=null) mDistLabel.setVisibility(View.INVISIBLE);
//RouteOverlay作成
mRouteOverlay = new RouteOverlay(this,"<あなたのアプリケーションID>");
//出発地ピン吹き出し設定
mRouteOverlay.setStartTitle("出発地");
//目的地ピン吹き出し設定
mRouteOverlay.setGoalTitle("目的地");
//出発地、目的地、移動手段を設定
mRouteOverlay.setRoutePos(mStartPos, mGoalPos, RouteOverlay.TRAFFIC_WALK);
//RouteOverlayListenerの設定
mRouteOverlay.setRouteOverlayListener(this);
//検索を開始
mRouteOverlay.search();
//MapViewにRouteOverlayを追加
mMapView.getOverlays().add(mRouteOverlay);
return false;
}
public boolean onTouch(MapView arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
//ルート検索エラー
public boolean errorRouteSearch(RouteOverlay arg0, int arg1) {
//プログレスダイアログを消します
if(mProgDialog!=null){
mProgDialog.dismiss();
mProgDialog = null;
}
return false;
}
//ルート検索完了
public boolean finishRouteSearch(RouteOverlay routeOverlay) {
//プログレスダイアログを消します
if(mProgDialog!=null){
mProgDialog.dismiss();
mProgDialog = null;
}
//距離テキストビューを表示
if(mDistLabel!=null){
mDistLabel.setVisibility(View.VISIBLE);
}else{
mDistLabel = new TextView(this);
this.addContentView(mDistLabel, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
mDistLabel.setTextSize(20);
mDistLabel.setTextColor(Color.argb(255, 255, 255, 255));
mDistLabel.setBackgroundColor(Color.argb(127, 0, 0, 0));
//距離を設定
mDistLabel.setText(String.format("距離 %.3fキロメートル",(routeOverlay.getDistance()/1000)));
return false;
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
