标签:android
因项目中要用到GPS定位,学习了下,便把学习成果记录下来。<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
LocationManger locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
class myListen implements LocationListener { @Override public void onLocationChanged(Location location) { //位置发生变化 Log.i(TAG, "onLocationChanged......."); if (mHandler != null) { Message msg = mHandler.obtainMessage(REFRESH_LOCATION, location); msg.sendToTarget(); } } @Override public void onProviderDisabled(String provider) { // } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // } }
myListenGPS = new myListen(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, CHECK_POSITION_INTERVAL, 0, myListenGPS);
* Location 位置信息,通过Location可以获取时间、经纬度、海拔等位置信息。
Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);//主动获取Location SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Date date = new Date(location.getTime()); String message = "时间:" + dateFormat.format(date) + "\n" +"经度:" + location.getLongitude() + "\n" + "纬度:" + location.getLatitude() + "\n";
标签:android
原文地址:http://blog.csdn.net/u011138654/article/details/39201675