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. mLocation01 = mLocationManager01.getLastKnownLocation
  14. (strLocationProvider);
  15. }
  16. catch(Exception e)
  17. {
  18. mTextView01.setText(e.toString());
  19. e.printStackTrace();
  20. }
  21. }
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. {
  3. GeoPoint gp = null;
  4. try
  5. {
  6. if (location != null)
  7. {
  8. double geoLatitude = location.getLatitude()*1E6;
  9. double geoLongitude = location.getLongitude()*1E6;
  10. gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
  11. }
  12. }
  13. catch(Exception e)
  14. {
  15. e.printStackTrace();
  16. }
  17. return gp;
  18. }
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. return false;
  12. }
@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. double geoLatitude = (int)gp.getLatitudeE6()/1E6;
  6. double geoLongitude = (int)gp.getLongitudeE6()/1E6;
  7. List<Address> lstAddress =
  8. gc.getFromLocation(geoLatitude, geoLongitude, 1);
  9. StringBuilder sb = new StringBuilder();
  10. if (lstAddress.size() > 0)
  11. {
  12. Address adsLocation = lstAddress.get(0);
  13. for(int i=0;i<adsLocation.getMaxAddressLineIndex();i++)
  14. {
  15. sb.append(adsLocation.getAddressLine(i)).append("\n");
  16. }
  17. sb.append(adsLocation.getLocality()).append("\n");
  18. sb.append(adsLocation.getPostalCode()).append("\n");
  19. sb.append(adsLocation.getCountryName());
  20. }
 //根据地理环境来确定编码 //注意这个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. List<Address> lstAddress = mGeocoder01.getFromLocationName
  11. (strSearchAddress, 1);
  12. if (!lstAddress.isEmpty())
  13. {
  14. Address adsLocation = lstAddress.get(0);
  15. double geoLatitude = adsLocation.getLatitude()*1E6;
  16. double geoLongitude = adsLocation.getLongitude()*1E6;
  17. gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
  18. }
  19. }
  20. }
  21. catch (Exception e)
  22. {
  23. e.printStackTrace();
  24. }
  25. return gp;
  26. }
 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. public void onClick(View v)
  5. {
  6. intZoomLevel++;
  7. if(intZoomLevel>mMapView01.getMaxZoomLevel())
  8. {
  9. intZoomLevel = mMapView01.getMaxZoomLevel();
  10. }
  11. mMapController01.setZoom(intZoomLevel);
  12. }
  13. });
  14. mButton03 = (Button)findViewById(R.id.myButton3);
  15. mButton03.setOnClickListener(new Button.OnClickListener()
  16. {
  17. public void onClick(View v)
  18. {
  19. intZoomLevel--;
  20. if(intZoomLevel<1)
  21. {
  22. intZoomLevel = 1;
  23. }
  24. mMapController01.setZoom(intZoomLevel);
  25. }
  26. });
 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

更多相关文章

  1. 读取android手机流量信息
  2. android EditText设置不可写
  3. android 使用html5作布局文件: webview跟javascript交互
  4. android studio调试c/c++代码
  5. IM-A820L限制GSM,WCDMA上网的原理(其他泛泰机型可参考)7.13
  6. 锁屏界面
  7. android(NDK+JNI)---Eclipse+CDT+gdb调试android ndk程序
  8. Android(安卓)version and Linux Kernel version
  9. Android(安卓)闹钟管理类的使用

随机推荐

  1. Android应用程序消息处理机制(Looper、Han
  2. Retrofit的简单使用
  3. Android(安卓)Zygote
  4. 分享一个好用的Android投屏工具-scrcpy
  5. Android(安卓)轮询最佳实践 Service + Al
  6. Android高手进阶教程(二)之----Android(
  7. android加载网络图片(逐行扫描格式png图
  8. Android属性系统
  9. 从零开始学android:Android中的基本控件(
  10. 根据css背景图属性+css定位制作雪碧图效