最後に取得した位置
Location location = mLocatioManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
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;
private final static String API_KEY = "0HrEFkF4qrKk7s-4S8RbWvKimK2sFJHUKRAkQJQ";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
base = new LinearLayout(context);
base.setOrientation(LinearLayout.VERTICAL);
setContentView(base);
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");
base.addView(tv);
base.addView(mapview);
}
@Override
protected void onResume() {
TODO
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
return false;
}
@Override
public void onLocationChanged(Location location) {
TODO
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
}
@Override
public void onProviderEnabled(String provider) {
TODO
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
TODO
}
}
Manifest.xml
<manifest xmlnsandroid="http://schemas.android.com/apk/res/android"
package="com.efolab.s098"
androidversionCode="1"
androidversionName="1.0" >
<uses-sdk
androidminSdkVersion="8"
androidtargetSdkVersion="17" />
<uses-permission androidname="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission androidname="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission androidname="android.permission.INTERNET"/>
<application
androidallowBackup="true"
androidicon="@drawable/ic_launcher"
androidlabel="@string/app_name"
androidtheme="@style/AppTheme" >
<activity androidname="Main">
<intent-filter>
<action androidname="android.intent.action.MAIN"/>
<category androidname="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<uses-library androidname="com.google.android.maps"/>
</application>
</manifest>