GPS開發常用方法 和用Criteria確定android location providerGPS開發常用方法

來源:互聯網
上載者:User
GPS常用方法總結

取得LocationProvider

Java代碼 public void getLocationProvider()   {   try   {   Criteria mCriteria01 = new Criteria();   mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);   mCriteria01.setAltitudeRequired( false);   mCriteria01.setBearingRequired( false);   mCriteria01.setCostAllowed( true);   mCriteria01.setPowerRequirement(Criteria.POWER_LOW);   strLocationProvider =   mLocationManager01.getBestProvider(mCriteria01, true);     mLocation01 = mLocationManager01.getLastKnownLocation   (strLocationProvider);   }   catch(Exception e)   {   mTextView01.setText(e.toString());   e.printStackTrace();   }   }
public void getLocationProvider() { try { Criteria mCriteria01 = new Criteria(); mCriteria01.setAccuracy(Criteria.ACCURACY_FINE); mCriteria01.setAltitudeRequired(false); mCriteria01.setBearingRequired(false); mCriteria01.setCostAllowed(true); mCriteria01.setPowerRequirement(Criteria.POWER_LOW); strLocationProvider = mLocationManager01.getBestProvider(mCriteria01, true); mLocation01 = mLocationManager01.getLastKnownLocation (strLocationProvider); } catch(Exception e) { mTextView01.setText(e.toString()); e.printStackTrace(); } }

擷取經緯度,並返回GeoPoint對象
Java代碼 private GeoPoint getGeoByLocation(Location location)  {  GeoPoint gp = nulltryif (location != null)  {  double geoLatitude = location.getLatitude()*1E6;  double geoLongitude = location.getLongitude()*1E6;  gp = new GeoPoint(( int) geoLatitude, ( int) geoLongitude);  }  }  catch(Exception e)  {  e.printStackTrace();  }  return gp;  }
private GeoPoint getGeoByLocation(Location location) { GeoPoint gp = null; try { if (location != null) { double geoLatitude = location.getLatitude()*1E6; double geoLongitude = location.getLongitude()*1E6; gp = new GeoPoint((int) geoLatitude, (int) geoLongitude); } } catch(Exception e) { e.printStackTrace(); } return gp; }

將經緯度轉換成實際螢幕座標
Java代碼 Point myScreenCoords = new Point();  GeoPoint tmpGeoPoint = new GeoPoint(( int)(mLocation.getLatitude()*1E6),( int)(mLocation.getLongitude()*1E6));  mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
Point myScreenCoords = new Point(); GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6)); mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);


點擊MapView任意一點獲得座標
Java代碼 @Override   public boolean onTouchEvent(MotionEvent ev) {   int actionType = ev.getAction();   switch (actionType) {   case MotionEvent.ACTION_UP:   Projection proj = mapView.getProjection();   GeoPoint loc = proj.fromPixels(( int)arg0.getX(), ( int)arg0.getY());   String sirina=Double.toString(loc.getLongitudeE6()/1000000);   String dolzina=Double.toString(loc.getLatitudeE6()/1000000);     }     return false;  }
@Override public boolean onTouchEvent(MotionEvent ev) { int actionType = ev.getAction(); switch (actionType) { case MotionEvent.ACTION_UP: Projection proj = mapView.getProjection(); GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY()); String sirina=Double.toString(loc.getLongitudeE6()/1000000); String dolzina=Double.toString(loc.getLatitudeE6()/1000000); } return false; }



經緯度改變來重新整理地圖
Java代碼 public void refreshMapView()   {   GeoPoint p = new GeoPoint(( int)(dLat* 1E6), ( int)(dLng* 1E6));   mMapView01.displayZoomControls( true);  mMapController01.animateTo(p);   mMapController01.setZoom(intZoomLevel);   }
 public void refreshMapView() { GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6)); mMapView01.displayZoomControls(true); mMapController01.animateTo(p); mMapController01.setZoom(intZoomLevel); }


根據當前的經緯度,擷取相關的一些地址資訊
Java代碼 //根據地理環境來確定編碼 //注意這個Locale是java.util.Locale包的類,擷取當前系統設定的語言 Geocoder gc = new Geocoder  (EX09_05. this, Locale.getDefault());    double geoLatitude = ( int)gp.getLatitudeE6()/1E6;  double geoLongitude = ( int)gp.getLongitudeE6()/1E6;    List<Address> lstAddress =   gc.getFromLocation(geoLatitude, geoLongitude, 1);    StringBuilder sb = new StringBuilder();    if (lstAddress.size() > 0)  {  Address adsLocation = lstAddress.get(0);  for( int i=0;i<adsLocation.getMaxAddressLineIndex();i++)  {  sb.append(adsLocation.getAddressLine(i)).append("\n");  }  sb.append(adsLocation.getLocality()).append("\n");  sb.append(adsLocation.getPostalCode()).append("\n");  sb.append(adsLocation.getCountryName());  } 
 //根據地理環境來確定編碼 //注意這個Locale是java.util.Locale包的類,擷取當前系統設定的語言 Geocoder gc = new Geocoder (EX09_05.this, Locale.getDefault()); double geoLatitude = (int)gp.getLatitudeE6()/1E6; double geoLongitude = (int)gp.getLongitudeE6()/1E6; List<Address> lstAddress = gc.getFromLocation(geoLatitude, geoLongitude, 1); StringBuilder sb = new StringBuilder(); if (lstAddress.size() > 0) { Address adsLocation = lstAddress.get(0); for(int i=0;i<adsLocation.getMaxAddressLineIndex();i++) { sb.append(adsLocation.getAddressLine(i)).append("\n"); } sb.append(adsLocation.getLocality()).append("\n"); sb.append(adsLocation.getPostalCode()).append("\n"); sb.append(adsLocation.getCountryName()); }



根據輸入地址,取得其GeoPoint對象
Java代碼 private GeoPoint getGeoByAddress(String strSearchAddress)   {   GeoPoint gp = null;   try   {   if(strSearchAddress!="")   {   Geocoder mGeocoder01 = new Geocoder   (EX09_07. this, Locale.getDefault());     List<Address> lstAddress = mGeocoder01.getFromLocationName  (strSearchAddress, 1);  if (!lstAddress.isEmpty())   {   Address adsLocation = lstAddress.get(0);   double geoLatitude = adsLocation.getLatitude()*1E6;   double geoLongitude = adsLocation.getLongitude()*1E6;   gp = new GeoPoint(( int) geoLatitude, ( int) geoLongitude);   }   }   }   catch (Exception e)   {   e.printStackTrace();   }   return gp;   }
 private GeoPoint getGeoByAddress(String strSearchAddress) { GeoPoint gp = null; try { if(strSearchAddress!="") { Geocoder mGeocoder01 = new Geocoder (EX09_07.this, Locale.getDefault()); List<Address> lstAddress = mGeocoder01.getFromLocationName (strSearchAddress, 1); if (!lstAddress.isEmpty()) { Address adsLocation = lstAddress.get(0); double geoLatitude = adsLocation.getLatitude()*1E6; double geoLongitude = adsLocation.getLongitude()*1E6; gp = new GeoPoint((int) geoLatitude, (int) geoLongitude); } } } catch (Exception e) { e.printStackTrace(); } return gp; }


地圖放大縮小按鈕
Java代碼 mButton02 = (Button)findViewById(R.id.myButton2);   mButton02.setOnClickListener( new Button.OnClickListener()   {     public void onClick(View v)   {   intZoomLevel++;   if(intZoomLevel>mMapView01.getMaxZoomLevel())   {   intZoomLevel = mMapView01.getMaxZoomLevel();   }   mMapController01.setZoom(intZoomLevel);   }   });     mButton03 = (Button)findViewById(R.id.myButton3);   mButton03.setOnClickListener( new Button.OnClickListener()   {     public void onClick(View v)   {   intZoomLevel--;   if(intZoomLevel<1)   {   intZoomLevel = 1;   }   mMapController01.setZoom(intZoomLevel);   }   });
 mButton02 = (Button)findViewById(R.id.myButton2); mButton02.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { intZoomLevel++; if(intZoomLevel>mMapView01.getMaxZoomLevel()) { intZoomLevel = mMapView01.getMaxZoomLevel(); } mMapController01.setZoom(intZoomLevel); } }); mButton03 = (Button)findViewById(R.id.myButton3); mButton03.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { intZoomLevel--; if(intZoomLevel<1) { intZoomLevel = 1; } mMapController01.setZoom(intZoomLevel); } });


以下文章轉載: http://marshal.easymorse.com/archives/2528
android location provider有兩個:

* LocationManager.GPS_PROVIDER:GPS,精度比較高,但是慢而且消耗電力,而且可能因為天氣原因或者障礙物而無法擷取衛星資訊,另外裝置可能沒有GPS模組;
* LocationManager.NETWORK_PROVIDER:通過網路擷取定位資訊,精度低,耗電少,擷取資訊速度較快,不依賴GPS模組。

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



這裡使用到了Criteria,可根據當前裝置情況自動選擇哪種location provider。見
Java代碼 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  Criteria criteria = new Criteria();  criteria.setAccuracy(Criteria.ACCURACY_FINE);// 設定為最大精度 criteria.setAltitudeRequired( false);//不要求海拔資訊 criteria.setBearingRequired( false);// 不要求方位資訊 criteria.setCostAllowed( true);//是否允許付費 criteria.setPowerRequirement(Criteria.POWER_LOW);// 對電量的要求 location = locationManager  .getLastKnownLocation(locationManager.getBestProvider(criteria, true));
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE);// 設定為最大精度 criteria.setAltitudeRequired(false);//不要求海拔資訊 criteria.setBearingRequired(false);// 不要求方位資訊 criteria.setCostAllowed(true);//是否允許付費 criteria.setPowerRequirement(Criteria.POWER_LOW);// 對電量的要求 location = locationManager .getLastKnownLocation(locationManager.getBestProvider(criteria, true));

原來的寫法很簡單: Java代碼 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  location=locationManager.getLastKnownLocation(LocationManager.NETWORK

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.