Android GPS開發常用方法
GPS常用方法總結
取得LocationProvider
Java代碼
</div>
- 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代碼
</div>
- private GeoPoint getGeoByLocation(Location location)
- {
- GeoPoint gp = null;
- try
- {
- /* 當Location存在 */
- 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代碼
</div>
- Point myScreenCoords = new Point();
- GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
- mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
點擊MapView任意一點獲得坐標
Java代碼
</div>
- @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代碼
</div>
- public void refreshMapView()
- {
- GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6));
- mMapView01.displayZoomControls(true);
- /* 將Map的中點移至GeoPoint */
- mMapController01.animateTo(p);
- mMapController01.setZoom(intZoomLevel);
- }
根據當前的經緯度,獲取相關的一些地址信息
Java代碼
</div>
- /* 創建Geocoder對象 */
- //根據地理環境來確定編碼
- //注意這個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代碼
</div>
- 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代碼
</div>
- /* 放大Map的Button */
- 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);
- }
- });
- /* 縮小Map的Button */
- 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);
- }
- });
android location provider有兩個:
LocationManager.GPS_PROVIDER:GPS,精度比較高,但是慢而且消耗電力,而且可能因為天氣原因或者障礙物而無法獲取衛星信息,另外設備可能沒有GPS模塊;
LocationManager.NETWORK_PROVIDER:通過網絡獲取定位信息,精度低,耗電少,獲取信息速度較快,不依賴GPS模塊。
為了程序的通用性,希望動態選擇location provider。對android通過Location API顯示地址信息做了個別改動,可以看到使用了gps定位,精度較高:

這里使用到了Criteria,可根據當前設備情況自動選擇哪種location provider。見
Java代碼
</div>
- 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代碼
</div>
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- location=locationManager.getLastKnownLocation(LocationManager.NETWORK
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!