LoginSignup
2
2

More than 5 years have passed since last update.

訂正:Yahoo!AndroidマップSDKで現在位置を追跡表示する

Last updated at Posted at 2012-06-20

昨日公開したコードが、ちょっと微妙なので訂正版を公開します。

MylocationOverlayクラスは、LocationListenerをimplementsしているので、LocationManager#requestLocationUpdateからリスナークラスとして登録してあげれば目論見通りの動作をしてくれます。

また、実用性を考えるとLocationManager#lastKnownLocationとかも使っておいたほうがいいように思うので、最後に取得した位置情報が設定時間以内であればその値を採用するように設定しています。

という訳で、以下コード

MainActivity.java
public class ConvininenceSearchActivity extends ActionBarActivity {
    /** デバッグ用タグ */
    private static final String LOGTAG = ConvininenceSearchActivity.class
            .getSimpleName();
    /** Yahoo!Map SDK のID */
    private static final String YOLP_ID = "";
    /** LastKnowLocationで得られる情報の古さの限界。5分 */
    private static final long LAST_KNOWN_MIN = 5 * 60 * 1000L;
    /** 位置情報の更新間隔。60秒に1回更新 */
    private static final int MIN_TIME = 60 * 1000;
    /** 位置情報の更新間隔メートル.10mに1回更新 */
    private static final int MIN_DISTANCE = 10;

    /** 画面に表示するMapView */
    protected transient MapView mapView;
    /** 地図のコントローラー */
    protected transient MapController mapController;
    /** 現在位置を表示するオーバーレイ */
    protected transient NowLocationOverlay locationOverlay;
    /** ロケーションマネージャー。位置情報をマネジメント!! */
    protected transient LocationManager locationManager;

    /** Activity生成時により呼び出されるメソッド。地図の初期化なんかを行う。 */
    @Override
    public void onCreate(final Bundle sIState) {
        super.onCreate(sIState);
        mapView = new MapView(this, YOLP_ID);
        mapView.setBuiltInZoomControls(true);
        setContentView(mapView);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        final String provider = getProvider(locationManager);
        locationOverlay = new NowLocationOverlay(this, mapView);
        final Location lastKnown = getLastKnownLocation();
        if (lastKnown != null) {
            locationOverlay.moveMap(lastKnown, mapController);
        }
        locationManager.requestLocationUpdates(provider, MIN_TIME,
                MIN_DISTANCE, locationOverlay);
        mapView.getOverlays().add(locationOverlay);

    }

    /**
     * 最後に取得した緯度経度を得る。 5分以内の値であれば有効な値として採用する
     * 
     * @return 最後に取得した緯度経度
     */
    protected Location getLastKnownLocation() {
        Location resultLocation = null;
        final Location lastKnownLocation = locationManager
                .getLastKnownLocation(getProvider(locationManager));
        if (lastKnownLocation != null
                && (new Date().getTime() - lastKnownLocation.getTime()) < LAST_KNOWN_MIN) {
            resultLocation = new Location(lastKnownLocation);
        }
        return resultLocation;
    }

    /**
     * 位置選択のためのプロバイダを選択する.方位、速度、高度は不要であるためそれらを消去した上で、残ったものを選択する
     * 
     * @return 選択されたプロバイダ
     */
    protected String getProvider(final LocationManager locationManager) {
        final Criteria criteria = new Criteria();
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setAltitudeRequired(false);
        // return locationManager.getBestProvider(criteria, true);
        return LocationManager.GPS_PROVIDER;
    }

    // @Override
    // protected void onResume() {
    // super.onResume();
    // }

    @Override
    protected void onPause() {
        locationManager.removeUpdates(locationOverlay);
        super.onPause();
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}
NowLocationOverlay.java
public class NowLocationOverlay extends MyLocationOverlay {
    /** デバッグ用のタグ */
    private final transient String TAG = NowLocationOverlay.class
            .getSimpleName();
    /** 表示している地図 */
    private final transient MapView mapView;

    public NowLocationOverlay(final Context context, final MapView mapView) {
        super(context, mapView);
        // TODO Auto-generated constructor stub
        this.mapView = mapView;

    }

    @Override
    public void onLocationChanged(final Location location) {
        // TODO Auto-generated method stub
        Log.d(TAG, "on location changed");
        final MapController mapController = mapView.getMapController();

        moveMap(location, mapController);
        super.onLocationChanged(location);
    }

    /**
     * 地図の中心位置を変更する
     * 
     * @param location
     *            変更する位置情報
     * @param mapController
     *            マップコントロール
     */
    protected void moveMap(final Location location,
            final MapController mapController) {
        final int lat1E6 = (int) (location.getLatitude() * 1E6);
        final int lng1E6 = (int) (location.getLongitude() * 1E6);
        final GeoPoint nowPoint = new GeoPoint(lat1E6, lng1E6);
        mapController.animateTo(nowPoint);
        mapView.invalidate();
    }

}

さて、最近利用しているYahoo!AndroidマップSDKだけど、どうも地図をこねくり回していると(ものすごい勢いでズーム、スクロールすると)クラッシュする。

サンプルコードを改変せずにクラッシュするのだから、俺は悪くない(ぇ

相変わらず、フィードバックの提出先がわからないのであった。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2