Android GPS開發常用方法

fmms 12年前發布 | 39K 次閱讀 Android Android開發 移動開發

GPS常用方法總結 
取得LocationProvider
 

Java代碼  
</div>
  1. public void getLocationProvider()   
  2.   {   
  3.     try   
  4.     {   
  5.       Criteria mCriteria01 = new Criteria();   
  6.       mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);   
  7.       mCriteria01.setAltitudeRequired(false);   
  8.       mCriteria01.setBearingRequired(false);   
  9.       mCriteria01.setCostAllowed(true);   
  10.       mCriteria01.setPowerRequirement(Criteria.POWER_LOW);   
  11.       strLocationProvider =    
  12.       mLocationManager01.getBestProvider(mCriteria01, true);   
  13.          
  14.       mLocation01 = mLocationManager01.getLastKnownLocation   
  15.       (strLocationProvider);   
  16.     }   
  17.     catch(Exception e)   
  18.     {   
  19.       mTextView01.setText(e.toString());   
  20.       e.printStackTrace();   
  21.     }   
  22.   }   
</div>
獲取經緯度,并返回GeoPoint對象 
Java代碼  
</div>
  1. private GeoPoint getGeoByLocation(Location location)  
  2.   {  
  3.     GeoPoint gp = null;  
  4.     try  
  5.     {  
  6.       /* 當Location存在 */  
  7.       if (location != null)  
  8.       {  
  9.         double geoLatitude = location.getLatitude()*1E6;  
  10.         double geoLongitude = location.getLongitude()*1E6;  
  11.         gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);  
  12.       }  
  13.     }  
  14.     catch(Exception e)  
  15.     {  
  16.       e.printStackTrace();  
  17.     }  
  18.     return gp;  
  19.   }  
</div>
將經緯度轉換成實際屏幕坐標 
Java代碼  
</div>
  1. Point myScreenCoords = new Point();  
  2. GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));  
  3. mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);  
</div>

點擊MapView任意一點獲得坐標 
Java代碼  
</div>
  1. @Override   
  2. public boolean onTouchEvent(MotionEvent ev) {   
  3.     int actionType = ev.getAction();   
  4.     switch (actionType) {   
  5.     case MotionEvent.ACTION_UP:        
  6.             Projection proj = mapView.getProjection();   
  7.             GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY());    
  8.             String sirina=Double.toString(loc.getLongitudeE6()/1000000);   
  9.             String dolzina=Double.toString(loc.getLatitudeE6()/1000000);   
  10.       
  11.     }   
  12.    
  13.     return false;  
  14. }  
</div>


經緯度改變來刷新地圖 
Java代碼  
</div>
  1. public void refreshMapView()   
  2. {   
  3.   GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6));   
  4.   mMapView01.displayZoomControls(true);  
  5.   /* 將Map的中點移至GeoPoint */  
  6.   mMapController01.animateTo(p);   
  7.   mMapController01.setZoom(intZoomLevel);   
  8. }   
</div>

根據當前的經緯度,獲取相關的一些地址信息 
Java代碼  
</div>
  1. /* 創建Geocoder對象 */  
  2.         //根據地理環境來確定編碼  
  3.         //注意這個Locale是java.util.Locale包的類,獲取當前系統設定的語言  
  4.         Geocoder gc = new Geocoder  
  5.         (EX09_05.this, Locale.getDefault());  
  6.           
  7.         /* 取出地理坐標經緯度 */  
  8.         double geoLatitude = (int)gp.getLatitudeE6()/1E6;  
  9.         double geoLongitude = (int)gp.getLongitudeE6()/1E6;  
  10.           
  11.         /* 自經緯度取得地址(可能有多行地址) */  
  12.         List<Address> lstAddress =   
  13.         gc.getFromLocation(geoLatitude, geoLongitude, 1);  
  14.           
  15.         StringBuilder sb = new StringBuilder();  
  16.           
  17.         /* 判斷地址是否為多行 */  
  18.         if (lstAddress.size() > 0)  
  19.         {  
  20.           Address adsLocation = lstAddress.get(0);  
  21.   
  22.           for(int i=0;i<adsLocation.getMaxAddressLineIndex();i++)  
  23.           {  
  24.             sb.append(adsLocation.getAddressLine(i)).append("\n");  
  25.           }  
  26.           sb.append(adsLocation.getLocality()).append("\n");  
  27.           sb.append(adsLocation.getPostalCode()).append("\n");  
  28.           sb.append(adsLocation.getCountryName());  
  29.         }  
  30.           
</div>


根據輸入地址,取得其GeoPoint對象 
Java代碼  
</div>
  1. private GeoPoint getGeoByAddress(String strSearchAddress)   
  2. {   
  3.   GeoPoint gp = null;   
  4.   try   
  5.   {   
  6.     if(strSearchAddress!="")   
  7.     {   
  8.       Geocoder mGeocoder01 = new Geocoder   
  9.       (EX09_07.this, Locale.getDefault());   
  10.          
  11.       List<Address> lstAddress = mGeocoder01.getFromLocationName  
  12.                          (strSearchAddress, 1);  
  13.       if (!lstAddress.isEmpty())   
  14.       {   
  15.         Address adsLocation = lstAddress.get(0);   
  16.         double geoLatitude = adsLocation.getLatitude()*1E6;   
  17.         double geoLongitude = adsLocation.getLongitude()*1E6;   
  18.         gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);   
  19.       }   
  20.     }   
  21.   }   
  22.   catch (Exception e)   
  23.   {    
  24.     e.printStackTrace();    
  25.   }   
  26.   return gp;   
  27. }   
</div>

地圖放大縮小按鈕 
Java代碼  
</div>
  1. /* 放大Map的Button */  
  2.    mButton02 = (Button)findViewById(R.id.myButton2);   
  3.    mButton02.setOnClickListener(new Button.OnClickListener()   
  4.    {   
  5.       
  6.      public void onClick(View v)   
  7.      {   
  8.        intZoomLevel++;   
  9.        if(intZoomLevel>mMapView01.getMaxZoomLevel())   
  10.        {   
  11.          intZoomLevel = mMapView01.getMaxZoomLevel();   
  12.        }   
  13.        mMapController01.setZoom(intZoomLevel);   
  14.      }   
  15.    });   
  16.       
  17.    /* 縮小Map的Button */  
  18.    mButton03 = (Button)findViewById(R.id.myButton3);   
  19.    mButton03.setOnClickListener(new Button.OnClickListener()   
  20.    {   
  21.         
  22.      public void onClick(View v)   
  23.      {   
  24.        intZoomLevel--;   
  25.        if(intZoomLevel<1)   
  26.        {   
  27.          intZoomLevel = 1;   
  28.        }   
  29.        mMapController01.setZoom(intZoomLevel);   
  30.      }   
  31.    });  
</div>
android location provider有兩個: 

    LocationManager.GPS_PROVIDER:GPS,精度比較高,但是慢而且消耗電力,而且可能因為天氣原因或者障礙物而無法獲取衛星信息,另外設備可能沒有GPS模塊; 
   
LocationManager.NETWORK_PROVIDER:通過網絡獲取定位信息,精度低,耗電少,獲取信息速度較快,不依賴GPS模塊。 

為了程序的通用性,希望動態選擇location provider。對android通過Location API顯示地址信息做了個別改動,可以看到使用了gps定位,精度較高: 

d38467c5-217c-3131-87e5-1f2743f5a22a.png 

這里使用到了Criteria,可根據當前設備情況自動選擇哪種location provider。見 
Java代碼  
</div>
  1. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
  2.   
  3. Criteria criteria = new Criteria();  
  4. criteria.setAccuracy(Criteria.ACCURACY_FINE);// 設置為最大精度  
  5. criteria.setAltitudeRequired(false);//不要求海拔信息  
  6. criteria.setBearingRequired(false);// 不要求方位信息  
  7. criteria.setCostAllowed(true);//是否允許付費  
  8. criteria.setPowerRequirement(Criteria.POWER_LOW);// 對電量的要求  
  9.   
  10. location = locationManager  
  11.         .getLastKnownLocation(locationManager.getBestProvider(criteria, true));  
</div>
原來的寫法很簡單:
Java代碼  
</div>
  1. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
  2.   
  3. location=locationManager.getLastKnownLocation(LocationManager.NETWORK  

</div>

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!