Android編程實現(xiàn)GPS位置獲取的方法
更新時間:2017年07月14日 10:19:30 作者:只要你能好
這篇文章主要介紹了Android編程實現(xiàn)GPS位置獲取的方法,結(jié)合具體實例形式分析了Android針對GPS定位的常見操作技巧,需要的朋友可以參考下
本文實例講述了Android編程實現(xiàn)GPS位置獲取的方法。分享給大家供大家參考,具體如下:
public class GPSInfoService {
private static GPSInfoService mInstance;
private LocationManager locationManager;//定位服務(wù)
private GPSInfoService(Context context) {
// TODO Auto-generated constructor stub
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
public static GPSInfoService getInstance(Context context){
if(mInstance == null){
mInstance = new GPSInfoService(context);
}
return mInstance;
}
//注冊定位監(jiān)聽
public void registenerLocationChangeListener(){
//得到所有的定位服務(wù)
// List<String> providers = locationManager.getAllProviders();
// for(String provider:providers){
// Log.i("i", provider);
// }
//查詢條件
Criteria criteria = new Criteria();
//定位的精準度
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//海拔信息是否關(guān)注
criteria.setAltitudeRequired(false);
//對周圍的事情是否進行關(guān)心
criteria.setBearingRequired(false);
//是否支持收費的查詢
criteria.setCostAllowed(true);
//是否耗電
criteria.setPowerRequirement(Criteria.POWER_LOW);
//對速度是否關(guān)注
criteria.setSpeedRequired(false);
//得到最好的定位方式
String provider = locationManager.getBestProvider(criteria, true);
//注冊監(jiān)聽
locationManager.requestLocationUpdates(provider, 60000, 0, getListener());
}
//取消監(jiān)聽
public void unRegisterLocationChangeListener(){
locationManager.removeUpdates(getListener());
}
private MyLocationListener listener;
//得到定位的監(jiān)聽器
private MyLocationListener getListener(){
if(listener == null){
listener = new MyLocationListener();
}
return listener;
}
//得到上個地理位置
public String getLastLocation(){
return sp.getString("last_location", "");
}
private final class MyLocationListener implements LocationListener{
//位置的改變
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double latitude = location.getLatitude();//維度
double longitude = location.getLongitude();//經(jīng)度
String last_location = "jingdu: " + longitude + ",weidu:" + latitude;
Editor editor = sp.edit();
editor.putString("last_location", last_location);
editor.commit();
}
//gps衛(wèi)星有一個沒有找到
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
//某個設(shè)置被打開
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
//某個設(shè)置被關(guān)閉
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android開發(fā)入門與進階教程》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計有所幫助。
您可能感興趣的文章:
- Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
- Android GPS定位測試(附效果圖和示例)
- Android實現(xiàn)GPS定位代碼實例
- android通過gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- android手機獲取gps和基站的經(jīng)緯度地址實現(xiàn)代碼
- Android中GPS定位的用法實例
- Android中實現(xiàn)GPS定位的簡單例子
- Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法
- Android編程獲取GPS數(shù)據(jù)的方法詳解
- python獲取android設(shè)備的GPS信息腳本分享
- Android GPS定位詳解及實例代碼
- Android 定位系統(tǒng)(GPS)開發(fā)詳解
相關(guān)文章
Android?WindowManager深層理解view繪制實現(xiàn)流程
WindowManager是Android中一個重要的Service,是全局且唯一的。WindowManager繼承自ViewManager。WindowManager主要用來管理窗口的一些狀態(tài)、屬性、view增加、刪除、更新、窗口順序、消息收集和處理等2022-11-11
Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫的方法
這篇文章主要介紹了Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12

