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

GPS常用方法总结

取得LocationProvider
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对象
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;
  }

将经纬度转换成实际屏幕坐标
Point myScreenCoords = new Point();
GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);


点击MapView任意一点获得坐标
@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;
}



经纬度改变来刷新地图
  public void refreshMapView() 
  { 
    GeoPoint p = new GeoPoint((int)(dLat* 1E6), (int)(dLng* 1E6)); 
    mMapView01.displayZoomControls(true);
    /* 将Map的中点移至GeoPoint */
    mMapController01.animateTo(p); 
    mMapController01.setZoom(intZoomLevel); 
  } 


根据当前的经纬度,获取相关的一些地址信息
/* 创建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对象
  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; 
  } 


地图放大缩小按钮
 /* 放大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定位,精度较高:

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

这里使用到了Criteria,可根据当前设备情况自动选择哪种location provider。见
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));

原来的写法很简单:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

location=locationManager.getLastKnownLocation(LocationManager.NETWORK



你可能感兴趣的:(android,UP)