GPS开发常用方法 和用Criteria确定android location providerGPS开发常用方法

GPS常用方法总结

取得LocationProvider

Java代码
  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. }
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代码
  1. private GeoPoint getGeoByLocation(Location location) 
  2. GeoPoint gp = null
  3. try
  4. if (location != null
  5. double geoLatitude = location.getLatitude()*1E6; 
  6. double geoLongitude = location.getLongitude()*1E6; 
  7. gp = new GeoPoint((int) geoLatitude, (int) geoLongitude); 
  8. catch(Exception e) 
  9. e.printStackTrace(); 
  10. return gp; 
  11. }
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代码
  1. Point myScreenCoords = new Point(); 
  2. GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6)); 
  3. 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代码
  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. }
@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代码
  1. public void refreshMapView()  
  2. {  
  3. GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6));  
  4. mMapView01.displayZoomControls(true); 
  5. mMapController01.animateTo(p);  
  6. mMapController01.setZoom(intZoomLevel);  
  7. }
 public void refreshMapView() { GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6)); mMapView01.displayZoomControls(true); mMapController01.animateTo(p); mMapController01.setZoom(intZoomLevel); }


根据当前的经纬度,获取相关的一些地址信息
Java代码
  1. //根据地理环境来确定编码
  2. //注意这个Locale是java.util.Locale包的类,获取当前系统设定的语言
  3. Geocoder gc = new Geocoder 
  4. (EX09_05.this, Locale.getDefault()); 
  5.  
  6. double geoLatitude = (int)gp.getLatitudeE6()/1E6; 
  7. double geoLongitude = (int)gp.getLongitudeE6()/1E6; 
  8.  
  9. List<Address> lstAddress =  
  10. gc.getFromLocation(geoLatitude, geoLongitude, 1); 
  11.  
  12. StringBuilder sb = new StringBuilder(); 
  13.  
  14. if (lstAddress.size() > 0
  15. Address adsLocation = lstAddress.get(0); 
  16. for(int i=0;i<adsLocation.getMaxAddressLineIndex();i++) 
  17. sb.append(adsLocation.getAddressLine(i)).append("\n"); 
  18. sb.append(adsLocation.getLocality()).append("\n"); 
  19. sb.append(adsLocation.getPostalCode()).append("\n"); 
  20. 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代码
  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. }
 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代码
  1. mButton02 = (Button)findViewById(R.id.myButton2);  
  2. mButton02.setOnClickListener(new Button.OnClickListener()  
  3. {  
  4.  
  5. public void onClick(View v)  
  6. {  
  7. intZoomLevel++;  
  8. if(intZoomLevel>mMapView01.getMaxZoomLevel())  
  9. {  
  10. intZoomLevel = mMapView01.getMaxZoomLevel();  
  11. }  
  12. mMapController01.setZoom(intZoomLevel);  
  13. }  
  14. });  
  15.  
  16. mButton03 = (Button)findViewById(R.id.myButton3);  
  17. mButton03.setOnClickListener(new Button.OnClickListener()  
  18. {  
  19.  
  20. public void onClick(View v)  
  21. {  
  22. intZoomLevel--;  
  23. if(intZoomLevel<1)  
  24. {  
  25. intZoomLevel = 1;  
  26. }  
  27. mMapController01.setZoom(intZoomLevel);  
  28. }  
  29. });
 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代码
  1. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
  2. Criteria criteria = new Criteria(); 
  3. criteria.setAccuracy(Criteria.ACCURACY_FINE);// 设置为最大精度
  4. criteria.setAltitudeRequired(false);//不要求海拔信息
  5. criteria.setBearingRequired(false);// 不要求方位信息
  6. criteria.setCostAllowed(true);//是否允许付费
  7. criteria.setPowerRequirement(Criteria.POWER_LOW);// 对电量的要求
  8. location = locationManager 
  9. .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代码
  1. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
  2. location=locationManager.getLastKnownLocation(LocationManager.NETWORK
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值