Android編程實(shí)現(xiàn)GPS位置獲取的方法
本文實(shí)例講述了Android編程實(shí)現(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;
}
//注冊(cè)定位監(jiān)聽(tīng)
public void registenerLocationChangeListener(){
//得到所有的定位服務(wù)
// List<String> providers = locationManager.getAllProviders();
// for(String provider:providers){
// Log.i("i", provider);
// }
//查詢(xún)條件
Criteria criteria = new Criteria();
//定位的精準(zhǔn)度
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//海拔信息是否關(guān)注
criteria.setAltitudeRequired(false);
//對(duì)周?chē)氖虑槭欠襁M(jìn)行關(guān)心
criteria.setBearingRequired(false);
//是否支持收費(fèi)的查詢(xún)
criteria.setCostAllowed(true);
//是否耗電
criteria.setPowerRequirement(Criteria.POWER_LOW);
//對(duì)速度是否關(guān)注
criteria.setSpeedRequired(false);
//得到最好的定位方式
String provider = locationManager.getBestProvider(criteria, true);
//注冊(cè)監(jiān)聽(tīng)
locationManager.requestLocationUpdates(provider, 60000, 0, getListener());
}
//取消監(jiān)聽(tīng)
public void unRegisterLocationChangeListener(){
locationManager.removeUpdates(getListener());
}
private MyLocationListener listener;
//得到定位的監(jiān)聽(tīng)器
private MyLocationListener getListener(){
if(listener == null){
listener = new MyLocationListener();
}
return listener;
}
//得到上個(gè)地理位置
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)星有一個(gè)沒(méi)有找到
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
//某個(gè)設(shè)置被打開(kāi)
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
//某個(gè)設(shè)置被關(guān)閉
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android控件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android打開(kāi)GPS導(dǎo)航并獲取位置信息返回null解決方案
- Android GPS定位測(cè)試(附效果圖和示例)
- Android實(shí)現(xiàn)GPS定位代碼實(shí)例
- android通過(guò)gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- android手機(jī)獲取gps和基站的經(jīng)緯度地址實(shí)現(xiàn)代碼
- Android中GPS定位的用法實(shí)例
- Android中實(shí)現(xiàn)GPS定位的簡(jiǎn)單例子
- Android使用GPS獲取用戶地理位置并監(jiān)聽(tīng)位置變化的方法
- Android編程獲取GPS數(shù)據(jù)的方法詳解
- python獲取android設(shè)備的GPS信息腳本分享
- Android GPS定位詳解及實(shí)例代碼
- Android 定位系統(tǒng)(GPS)開(kāi)發(fā)詳解
相關(guān)文章
Android?WindowManager深層理解view繪制實(shí)現(xiàn)流程
WindowManager是Android中一個(gè)重要的Service,是全局且唯一的。WindowManager繼承自ViewManager。WindowManager主要用來(lái)管理窗口的一些狀態(tài)、屬性、view增加、刪除、更新、窗口順序、消息收集和處理等2022-11-11
Android支持國(guó)際化多語(yǔ)言那點(diǎn)事(支持8.0+)
我們?cè)陂_(kāi)發(fā)app可能會(huì)拓展國(guó)外市場(chǎng),那么對(duì)包含英語(yǔ)在內(nèi)的其它語(yǔ)言支持就很有必要了。這篇文章主要介紹了Android支持國(guó)際化多語(yǔ)言那點(diǎn)事(支持8.0+),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android實(shí)現(xiàn)京東首頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)京東首頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Android Toast實(shí)現(xiàn)全屏顯示
這篇文章主要為大家詳細(xì)介紹了Android Toast實(shí)現(xiàn)全屏顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫(kù)的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12
Flutter項(xiàng)目在 iOS14 啟動(dòng)崩潰的解決方法
這篇文章主要介紹了Flutter項(xiàng)目在 iOS14 啟動(dòng)崩潰的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

