Android - GPS座標存取

摘要:Android - GPS座標存取

先設定存取權限

在Androidmanifest.xml設定權限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

取得程式如下


public static void showGPS(Context ctx) {
	LocationManager lms = (LocationManager) ctx.getSystemService(ctx.LOCATION_SERVICE);
	Location location = lms.getLastKnownLocation(LocationManager.GPS_PROVIDER);
	Double longitude = location.getLongitude();
	Double latitude = location.getLatitude();
	Toast.makeText(this, "Lo = " + longitude + "; La = " + latitude ,Toast.LENGTH_SHORT).show();
}