本文實(shí)例講述了Android編程實(shí)現(xiàn)GPS位置獲取的方法。分享給大家供大家參考,具體如下:
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)博山,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18982081108public 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ù) // Listproviders = locationManager.getAllProviders(); // for(String provider:providers){ // Log.i("i", provider); // } //查詢條件 Criteria criteria = new Criteria(); //定位的精準(zhǔn)度 criteria.setAccuracy(Criteria.ACCURACY_FINE); //海拔信息是否關(guān)注 criteria.setAltitudeRequired(false); //對(duì)周圍的事情是否進(jìn)行關(guān)心 criteria.setBearingRequired(false); //是否支持收費(fèi)的查詢 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 } } }