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

来源:互联网 发布:android 文件存储数据 编辑:程序博客网 时间:2024/06/05 15:44
 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.   }   

获取经纬度,并返回GeoPoint对象
Java代码 复制代码 收藏代码
  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.   }  

将经纬度转换成实际屏幕坐标
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);  


点击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. }  



经纬度改变来刷新地图
Java代码 复制代码 收藏代码
  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. }   


根据当前的经纬度,获取相关的一些地址信息
Java代码 复制代码 收藏代码
  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.           



根据输入地址,取得其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. }   


地图放大缩小按钮
Java代码 复制代码 收藏代码
  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.    });  


以下文章转载: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.   
  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));  

原来的写法很简单:
Java代码 复制代码 收藏代码
  1. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
  2.   
  3. location=locationManager.getLastKnownLocation(LocationManager.NETWORK  
原创粉丝点击