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

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

取得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.   {   
  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.   }  
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代码 复制代码
  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.   /* 将Map的中点移至GeoPoint */  
  6.   mMapController01.animateTo(p);    
  7.   mMapController01.setZoom(intZoomLevel);    
  8. }   
  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代码 复制代码
  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.           
/* 创建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代码 复制代码
  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. /* 放大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.    });  
 /* 放大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); 
      } 
    });


以下文章转载: 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));  
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.   
  3. location=locationManager.getLastKnownLocation(LocationManager.NETWORK  
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

location=locationManager.getLastKnownLocation(LocationManager.NETWORK)

http://qq361119908.iteye.com/blog/665074
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值