android GPS编程

一、打开系统GPS服务

locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);       
     if (locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) 
     {        
      Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();   
      return;     
     }   

二、设置需要查询的服务信息(填充位置数据类Criteria)

//查询精度:高
     criteria.setAccuracy(Criteria.ACCURACY_FINE);
     // 是否查询海拔:是
     criteria.setAltitudeRequired(true);
     //是否查询方位角:是
     criteria.setBearingRequired(true);
     //是否允许付费
     criteria.setCostAllowed(true);   
     // 电量要求:底
     criteria.setPowerRequirement(Criteria.POWER_LOW);
     //是否查询速度:是
     criteria.setSpeedRequired(true);

将需要查询的信息设置为true

三、获取location

//获取位置提供者provider中的位置信息

provider = locationManager.getBestProvider(criteria, true);

四、设置位置监听器,当位置变化时实时更新GPS信息

locationManager.requestLocationUpdates(provider, 1000, 0,locationListener);//第二个参数是更新时间(单位毫秒),第二个参数为更新的距离,最后参数为监听函数

五、设置监听函数

 //定位监听类负责监听位置信息的变化情况
    private final LocationListener locationListener = new LocationListener()
 {

  @Override
  public void onLocationChanged(Location location)
  {
    //   通过GPS获取位置    
      updateToNewLocation(location);    //展示GPS信息
  }

  @Override
  public void onProviderDisabled(String arg0)
  {
   
  }

  @Override
  public void onProviderEnabled(String arg0)
  {
  
  }

  @Override
  public void onStatusChanged(String arg0, int arg1, Bundle arg2)
  {
      updateToNewLocation(null);  
  }
 };

 

六、展示GPS信息

  private void updateToNewLocation(Location location)
    {      

     if (location != null)
     {          
      bear = location.getBearing(); //偏离正北方的度数
      double latitude = location.getLatitude();      //维度
      double longitude= location.getLongitude();     //经度 
      double GpsSpeed = location.getSpeed(); //速度 
      long GpsTime = location.getTime(); //时间
      Date date = new Date(GpsTime);
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      
      float GpsAlt = (float)location.getAltitude();  //海拔
      latitudeview.setText("" + latitude);
      longitudeview.setText("" + longitude);
      speedview.setText(""+GpsSpeed);
      timeview.setText(""+df.format(date));
      altitudeview.setText(""+GpsAlt);
      bearingview.setText(""+bear);
      }
      else
      {    
       Toast.makeText(this, "无法获取地理信息", Toast.LENGTH_SHORT).show();
      } 

}

七、除了主程序之外,要获取GPS信息,还得在manifest文件中添加权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="zfy.mygps"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name"
      android:debuggable="true">
        <activity android:name=".Mygps"
                  android:label="@string/app_name"
                  android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
       <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
</manifest>

 

 

获取卫星信息,同样是用LocationManager类,用其中的addGpsStatusListener()进行监听:

locationManager.addGpsStatusListener(statusListener)。参数为监听函数

 

 

监听函数:

 //添加监听卫星
    private final GpsStatus.Listener statusListener= new GpsStatus.Listener(){
  @Override
  public void onGpsStatusChanged(int event) {
   // TODO Auto-generated method stub
   //获取GPS卫星信息
   gpsStatus = locationManager.getGpsStatus(null);
   switch(event)
   {
   case GpsStatus.GPS_EVENT_STARTED:
   break;
    //第一次定位时间
   case GpsStatus.GPS_EVENT_FIRST_FIX: 
   break;
    //收到的卫星信息
   case GpsStatus.GPS_EVENT_SATELLITE_STATUS:

{

//获取所有卫星
  allSatellites = gpsStatus.getSatellites();

//得到卫星序列
  Iteratorsate = allSatellites.iterator();

while(Iteratorsate.hasNext())
  {
   GpsSatellite satellite = Iteratorsate.next();
     satellite.getAzimuth();//卫星方位角
     satellite.getElevation();//卫星仰角
    satellite.getSnr();//信噪比

   satellite.getPrn();//伪随机数
  }

}
   break;
   case GpsStatus.GPS_EVENT_STOPPED:
   break;
   }
  }
 };

posted @ 2012-02-06 11:04  OYK  阅读(1635)  评论(0编辑  收藏  举报