Android GPS应用:动态获取位置信息

在上文中,介绍了GPS概念及Android开发GPS应用涉及到的常用类和方法。在本文中,开发一个小应用,实时获取定位信息,包括用户所在的纬度、经度、高度、方向、移动速度等。代码如下:

Activity:

[java] view plaincopy
 
  1. package comhome.location;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.location.Location;  
  6. import android.location.LocationListener;  
  7. import android.location.LocationManager;  
  8. import android.os.Bundle;  
  9. import android.widget.EditText;  
  10.   
  11. public class LocationTestActivity extends Activity {  
  12.     // 定义LocationManager对象  
  13.     private LocationManager locationManager;  
  14.     private EditText show;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         show = (EditText) findViewById(R.id.main_et_show);  
  21.         // 获取系统LocationManager服务  
  22.         locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
  23.         // 从GPS获取最近的定位信息  
  24.         Location location = locationManager  
  25.                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  26.         // 将location里的位置信息显示在EditText中  
  27.         updateView(location);  
  28.         // 设置每2秒获取一次GPS的定位信息  
  29.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,  
  30.                 20008new LocationListener() {  
  31.   
  32.                     @Override  
  33.                     public void onLocationChanged(Location location) {  
  34.                         // 当GPS定位信息发生改变时,更新位置  
  35.                         updateView(location);  
  36.                     }  
  37.   
  38.                     @Override  
  39.                     public void onProviderDisabled(String provider) {  
  40.                         updateView(null);  
  41.                     }  
  42.   
  43.                     @Override  
  44.                     public void onProviderEnabled(String provider) {  
  45.                         // 当GPS LocationProvider可用时,更新位置  
  46.                         updateView(locationManager  
  47.                                 .getLastKnownLocation(provider));  
  48.   
  49.                     }  
  50.   
  51.                     @Override  
  52.                     public void onStatusChanged(String provider, int status,  
  53.                             Bundle extras) {  
  54.                     }  
  55.                 });  
  56.     }  
  57.   
  58.     private void updateView(Location location) {  
  59.         if (location != null) {  
  60.             StringBuffer sb = new StringBuffer();  
  61.             sb.append("实时的位置信息:\n经度:");  
  62.             sb.append(location.getLongitude());  
  63.             sb.append("\n纬度:");  
  64.             sb.append(location.getLatitude());  
  65.             sb.append("\n高度:");  
  66.             sb.append(location.getAltitude());  
  67.             sb.append("\n速度:");  
  68.             sb.append(location.getSpeed());  
  69.             sb.append("\n方向:");  
  70.             sb.append(location.getBearing());  
  71.             sb.append("\n精度:");  
  72.             sb.append(location.getAccuracy());  
  73.             show.setText(sb.toString());  
  74.         } else {  
  75.             // 如果传入的Location对象为空则清空EditText  
  76.             show.setText("");  
  77.         }  
  78.     }  
  79.   
  80. }  

布局XML:

[html] view plaincopy
 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" >  
  4.   
  5.     <EditText  
  6.         android:id="@+id/main_et_show"  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent"  
  9.         android:cursorVisible="false"  
  10.         android:editable="false" />  
  11.   
  12. </LinearLayout>  

权限:

[html] view plaincopy
 
  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  

附上图片效果:


如果把该程序与Google Map结合,让程序根据GPS提供的信息实时地显示用户在地图上的位置,即可开发出GPS导航系统。

posted on 2014-03-22 20:59  zhengwen  阅读(817)  评论(0编辑  收藏  举报