たぼさんの部屋

いちょぼとのんびり

SECTION098 最後に取得した位置情報を調べる

f:id:donsuka_kk:20121211113649p:plain

最後に取得した位置

//最後に取得した位置情報の取得
Location location = mLocatioManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Main.java

package com.efolab.s098;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

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 Main extends MapActivity implements LocationListener{

	Context context;
	LinearLayout base;
	
	LocationManager mLocatioManager;
	MapController mMapController;
	
	TextView tv;
	
	//MapView
	MapView mapview;
	private final static String API_KEY = "0HrEFkF4qrKk7s-4S8RbWvKimK2sFJHUKRAkQJQ"; // mac
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    context = getApplicationContext();
	    base = new LinearLayout(context);
	    base.setOrientation(LinearLayout.VERTICAL);
	    setContentView(base);
	    
	    //MapView
	    mapview = new MapView(this , API_KEY);
	    //位置の設定
	    mMapController = mapview.getController();
	    //ズームコントローラを配置する
	    mapview.getController().setZoom(16);
	    mapview.setBuiltInZoomControls(true);
	    mapview.invalidate();
	    //ロケーションマネージャの取得
	    mLocatioManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
	    mLocatioManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, this);
	    tv = new TextView(context);
	    tv.setText("onCreate");
	    //layout addView
	    base.addView(tv);
	    base.addView(mapview);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//最後に取得した位置情報の取得
		Location location = mLocatioManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		//テキストビュー
		if(location != null){
			double lat = location.getLatitude();
			double lng = location.getLongitude();
			GeoPoint gp = new GeoPoint((int)(lat * 1e6) , (int)(lng * 1e6));
			//地図を取得した位置へ移動
			mMapController.animateTo(gp);
			//最後に取得した位置をテキストビューへ出力
			tv.setText("最後に取得した:緯度="+lat+",経度="+lng);
		}else{
			tv.setText("位置情報が取得されていません");
		}
	}

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

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		double lat = location.getLatitude();
		double lng = location.getLongitude();
		GeoPoint gp = new GeoPoint((int)(lat * 1e6) , (int)(lng * 1e6));
		//地図を取得した位置へ移動
		mMapController.animateTo(gp);
		//最後に取得した位置をテキストビューへ出力
		tv.setText("緯度="+lat+",経度="+lng);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.efolab.s098"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
	<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
	<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="Main">
            <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>

</manifest>