2025-02-10

  GPS(Gobal Positional System)全球定位系統,是一個中距離圓型軌道衛星導航系統,他可以為地球表面的絕大部分地區(98%)提供準備的定位、測速和高精度的時間標準。

 

       Android支持地理定位服務的API。該地理定位服務可以用來獲取當前設備的地理位置,應用程序可以定時請求更新設備當前的地理定位信息。比如應用程序可以借助一個Intent接受器來實現如下功能:以經緯度和半徑劃定一個區域,當設備出入該區域時,發出提示信息,還可以和Google Map API一起使用,完成更多的任務。關於地理定位系統的API全部位於android.location包內,其中包括以下幾個重要的功能類:

  

類名 描述
LocationManager 提供訪問定位服務的功能,也提供獲取最佳定位提供者的功能,另外,臨時報警功能也可以借助該類來實現。
LocationProvider 定位提供者的抽象類。定位提供者具備周期性報告設備地理位置的功能。
LocationListener 提供定位信息發生改變時的回調共嫩。必須事先在定位管理器中註冊監聽器對象。
Criteria 使得應用能夠通過LocationProvider中設置的屬性來選擇合適的定位提供者。
Geocoder 用於處理地理編碼和反向地理編碼的類。地理編碼是指將地址或其他描述轉變為經度和緯度,反向地理編碼則是將經度和緯度轉變為地址或描述語言,其中包含瞭兩個構造函數,需要傳入經度和緯度的坐標。getFromLocation方法可以得到一組關於地址的數組。

      
        要使用地理定位,首先需要取得LocationManager的實例,在Android中,獲得LocationManager的唯一方法是通過getSystemService方法的調用。通過使用LocationManager,我們可以獲得一個位置提供者的列表。在一個真實的手持設備中,這個列表包含瞭一些GPS服務。我們也可以選擇更強大、更精確、不帶其他附加服務的GPS。代碼如下:
LocationManager locationManager;
        Stringcontext = Context.LOCATION_SERVICE;
        locationManager= (LocationManager)getSystemService(context);
取得LocationManager對象之後,我們還需要註冊一個周期的更新視圖,代碼如下
LocationManager.requestLocationUpdate(LocationManager.GPS_PROVDER,1000, 0, locationListener);
其中第一個參數是設置服務提供者,第二個參數是周期,最後一個參數locationListener,是用來監聽定位信息的改變,必須要實現如下方法:

方法 描述
onLocationChanged(Location location) 當坐標改變時候觸發該函數,如果Provider傳相同的坐標,它就不會觸發。
onProviderDisabled(String provider) Provider禁用時觸發此函數,比如GPS被關閉。
onProviderEnabled(String provider) Provider啟用時觸發此函數,比如GPS被打開。
onStatusChanged(String provider, int status, Bundle extras) Provider的轉態在可用、暫時不可用和無服務三個狀態直接切換時觸發此函數。

要使用定位的API,首先需要再AndroidManifest.xml文件中添加其權限,具體代碼如下:
 

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
         
        <uses-library  android:name="com.google.android.maps"/> 
         
        <activity 
            android:name=".GPSActivity" 
            android:label="@string/app_name" > 
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
       
        <uses-library  android:name="com.google.android.maps"/>
       
        <activity
            android:name=".GPSActivity"
            android:label="@string/app_name" >

 

 

由於我們在模擬器上測試,所以需要人為的設置一個坐標。可用通過兩種方法來設置一個模擬的坐標值。第一種方法是通過DDMS,我們可用在Eclipse的ADT插件中使用這種方法,隻要啟動Eclipse,選擇“Window”->“Show View”,打開“Emulator Control”界面手動或者通過KML和GPX文件來設置一個坐標。

圖片

另外一種方法使用geo命令,我們需要telnet到本機的5554端口,然後再命令行下輸入類似於geo fix-121.45365 46.51119 4392這樣的命令,後面三個參數分別是經度、緯度和(可選)海拔。設置後再Android模擬器屏幕上便多出瞭一個如圖9-17所示的標準,表示模擬瞭一個GPS權限。

圖片

現在我們可以使用位置管理器(LocationManager)和位置提供者進行getFromLocation的調用。這個方法放回本機當前位置的一個快照,這個快照將以Location對象形式提供。在手持設備中,我們可以獲得當前位置的經度和緯度;調用getFromLocationName方法可以返回一個數據表示一個地方的地名。

在這個地圖中,我們還可以創建瞭一個菜單來縮放地圖,這個功能是使用地圖控制器(MapController)的zoomIn和zoomOut方法來放大和縮小地圖。

下面試測試一個示例代碼:

 

[java] package cn.edu.pku; 
 
import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 
 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 
 
public class GPSActivity extends MapActivity { 
 
    public MapController mapController; 
    public MyLocationOverlay myPosition; 
    public MapView myMapView; 
    public static final int ZOOM_IN = Menu.FIRST; 
    public static final int ZOOM_OUT = Menu.FIRST + 1; 
     
    @Override 
    protected void onCreate(Bundle icicle) { 
        // TODO Auto-generated method stub  
        super.onCreate(icicle); 
         
        setContentView(R.layout.main); 
         
        LocationManager locationManager; 
        String context = Context.LOCATION_SERVICE; 
        locationManager = (LocationManager)getSystemService(context); 
        myMapView = (MapView)findViewById(R.id.mapView1); 
         
        mapController = myMapView.getController(); 
         
        //設置顯示模式  
        myMapView.setSatellite(true); 
        myMapView.setStreetView(true); 
         
        //設置縮放控制,這裡使用自己實現的縮放菜單  
        myMapView.displayZoomControls(false); 
        //設置使用MyLocationOverlay繪圖  
        mapController.setZoom(17); 
        myPosition = new MyLocationOverlay(); 
        List<Overlay> overlays = myMapView.getOverlays(); 
        overlays.add(myPosition); 
        //設置Criteria(服務商)的信息  
        Criteria criteria = new Criteria(); 
        //經度要求  
        criteria.setAccuracy(Criteria.ACCURACY_FINE); 
        criteria.setAltitudeRequired(false); 
        criteria.setBearingRequired(false); 
        criteria.setCostAllowed(false); 
        criteria.setPowerRequirement(Criteria.POWER_LOW); 
        //取得最好效果的criteria  
        String provider = locationManager.getBestProvider(criteria, true); 
        //獲得坐標相應信息  
        Location location = locationManager.getLastKnownLocation(provider); 
        //更新坐標相關信息  
        updateWithNewLocation(location); 
        //註冊一個周期的更新,3000ms更新一次  
        //locationManager用來監聽定位信息的改變  
        locationManager.requestLocationUpdates(provider, 3000, 0, locationListener); 
         
    } 
 
    private void updateWithNewLocation(Location location){ 
        String latLongString; 
        TextView myLocationText = (TextView)findViewById(R.id.textView1); 
        String addressString = "沒有找到地址\n"; 
         
        if(location != null){ 
            //為繪制標志的類設置坐標  
            //myPosition.  
            //取得經度和緯度  
            Double geoLat = location.getLatitude() * 1E6; 
            Double geoLng = location.getLongitude() * 1E6; 
             
            GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue()); 
            //定位到指定坐標  
            mapController.animateTo(point); 
            double lat = location.getLatitude(); 
            double lng = location.getLongitude(); 
            latLongString = "經度:" + lat + "\n緯度:" + lng; 
             
            double latitude = location.getLatitude(); 
            double longitude = location.getLongitude(); 
            //根據地理環境來確定編碼  
            Geocoder gc = new Geocoder(this, Locale.getDefault()); 
            try{ 
                //取得地址相關的一些信息、經度、緯度  
                List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); 
                StringBuilder sb = new StringBuilder(); 
                if(addresses.size() > 0){ 
                    Address address = addresses.get(0); 
                    for(int i = 0; i < address.getMaxAddressLineIndex(); i++){ 
                        sb.append(address.getAddressLine(i)).append("\n"); 
                        sb.append(address.getLocality()).append("\n"); 
                        sb.append(address.getPostalCode()).append("\n"); 
                        sb.append(address.getCountryName()); 
                        addressString = sb.toString(); 
                    } 
                }                
            }catch(IOException e){} 
        }else{ 
            latLongString = "沒有找到坐標. \n"; 
        } 
         
        myLocationText.setText("你當前的坐標如下:\n" + latLongString + "\n" + addressString); 
    } 
     
    private final LocationListener locationListener = new LocationListener() { 
         
        public void onStatusChanged(String provider, int status, Bundle extras) {//Provider轉態在可用、暫時不可服務和無服務三個狀態直接切換時觸發此函數  
            // TODO Auto-generated method stub  
             
        } 
         
        public void onProviderEnabled(String provider) {//Provider啟用時觸發此函數,比如GPS被打開  
            // TODO Auto-generated method stub  
             
        } 
         
        public void onProviderDisabled(String provider) {//Provider禁用時觸發此函數,比如GPS被關閉  
            // TODO Auto-generated method stub  
            updateWithNewLocation(null); 
        } 
         
        public void onLocationChanged(Location location) {//當坐標改變時觸發事件  
            // TODO Auto-generated method stub  
            updateWithNewLocation(location); 
        } 
    }; 
     
    @Override 
    protected boolean isRouteDisplayed() { 
        // TODO Auto-generated method stub  
        return false; 
    } 
 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        // TODO Auto-generated method stub  
        super.onOptionsItemSelected(item); 
         
        switch(item.getItemId()){ 
        case ZOOM_IN: 
            mapController.zoomIn(); 
            return true; 
        case ZOOM_OUT: 
            mapController.zoomOut(); 
            return true; 
        } 
         
        return true; 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // TODO Auto-generated method stub  
        super.onCreateOptionsMenu(menu); 
         
        menu.add(0, ZOOM_IN, Menu.NONE, "放大"); 
        menu.add(0, ZOOM_OUT, Menu.NONE, "縮小"); 
        return true; 
    } 
     
    class MyLocationOverlay extends Overlay{ 
        Location mLocation; 
        //更新坐標時,設置該坐標,以便畫圖  
        public void setLocation(Location location){ 
            mLocation = location; 
        } 
         
        @Override 
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
                long when) { 
            // TODO Auto-generated method stub  
            super.draw(canvas, mapView, shadow, when); 
             
            Paint paint = new Paint(); 
            Point myScreenCoords = new Point(); 
            //將經緯度轉換成實際屏幕坐標  
            GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude() * 1E6), (int)(mLocation.getLongitude() * 1E6)); 
            mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords); 
            paint.setStrokeWidth(1); 
            paint.setARGB(255, 255, 0, 0); 
            paint.setStyle(Paint.Style.STROKE); 
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home); 
            canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint); 
            canvas.drawText("Here am I", myScreenCoords.x, myScreenCoords.y, paint); 
             
            return true; 
        }        
    } 

package cn.edu.pku;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class GPSActivity extends MapActivity {

 public MapController mapController;
 public MyLocationOverlay myPosition;
 public MapView myMapView;
 public static final int ZOOM_IN = Menu.FIRST;
 public static final int ZOOM_OUT = Menu.FIRST + 1;
 
 @Override
 protected void onCreate(Bundle icicle) {
  // TODO Auto-generated method stub
  super.onCreate(icicle);
  
  setContentView(R.layout.main);
  
  LocationManager locationManager;
  String context = Context.LOCATION_SERVICE;
  locationManager = (LocationManager)getSystemService(context);
  myMapView = (MapView)findViewById(R.id.mapView1);
  
  mapController = myMapView.getController();
  
  //設置顯示模式 www.aiwalls.com
  myMapView.setSatellite(true);
  myMapView.setStreetView(true);
  
  //設置縮放控制,這裡使用自己實現的縮放菜單
  myMapView.displayZoomControls(false);
  //設置使用MyLocationOverlay繪圖
  mapController.setZoom(17);
  myPosition = new MyLocationOverlay();
  List<Overlay> overlays = myMapView.getOverlays();
  overlays.add(myPosition);
  //設置Criteria(服務商)的信息
  Criteria criteria = new Criteria();
  //經度要求
  criteria.setAccuracy(Criteria.ACCURACY_FINE);
  criteria.setAltitudeRequired(false);
  criteria.setBearingRequired(false);
  criteria.setCostAllowed(false);
  criteria.setPowerRequirement(Criteria.POWER_LOW);
  //取得最好效果的criteria
  String provider = locationManager.getBestProvider(criteria, true);
  //獲得坐標相應信息
  Location location = locationManager.getLastKnownLocation(provider);
  //更新坐標相關信息
  updateWithNewLocation(location);
  //註冊一個周期的更新,3000ms更新一次
  //locationManager用來監聽定位信息的改變
  locationManager.requestLocationUpdates(provider, 3000, 0, locationListener);
  
 }

 private void updateWithNewLocation(Location location){
  String latLongString;
  TextView myLocationText = (TextView)findViewById(R.id.textView1);
  String addressString = "沒有找到地址\n";
  
  if(location != null){
   //為繪制標志的類設置坐標
   //myPosition.
   //取得經度和緯度
   Double geoLat = location.getLatitude() * 1E6;
   Double geoLng = location.getLongitude() * 1E6;
   
   GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
   //定位到指定坐標
   mapController.animateTo(point);
   double lat = location.getLatitude();
   double lng = location.getLongitude();
   latLongString = "經度:" + lat + "\n緯度:" + lng;
   
   double latitude = location.getLatitude();
   double longitude = location.getLongitude();
   //根據地理環境來確定編碼
   Geocoder gc = new Geocoder(this, Locale.getDefault());
   try{
    //取得地址相關的一些信息、經度、緯度
    List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
    StringBuilder sb = new StringBuilder();
    if(addresses.size() > 0){
     Address address = addresses.get(0);
     for(int i = 0; i < address.getMaxAddressLineIndex(); i++){
      sb.append(address.getAddressLine(i)).append("\n");
      sb.append(address.getLocality()).append("\n");
      sb.append(address.getPostalCode()).append("\n");
      sb.append(address.getCountryName());
      addressString = sb.toString();
     }
    }    
   }catch(IOException e){}
  }else{
   latLongString = "沒有找到坐標. \n";
  }
  
  myLocationText.setText("你當前的坐標如下:\n" + latLongString + "\n" + addressString);
 }
 
 private final LocationListener locationListener = new LocationListener() {
  
  public void onStatusChanged(String provider, int status, Bundle extras) {//Provider轉態在可用、暫時不可服務和無服務三個狀態直接切換時觸發此函數
   // TODO Auto-generated method stub
   
  }
  
  public void onProviderEnabled(String provider) {//Provider啟用時觸發此函數,比如GPS被打開
   // TODO Auto-generated method stub
   
  }
  
  public void onProviderDisabled(String provider) {//Provider禁用時觸發此函數,比如GPS被關閉
   // TODO Auto-generated method stub
   updateWithNewLocation(null);
  }
  
  public void onLocationChanged(Location location) {//當坐標改變時觸發事件
   // TODO Auto-generated method stub
   updateWithNewLocation(location);
  }
 };
 
 @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // TODO Auto-generated method stub
  super.onOptionsItemSelected(item);
  
  switch(item.getItemId()){
  case ZOOM_IN:
   mapController.zoomIn();
   return true;
  case ZOOM_OUT:
   mapController.zoomOut();
   return true;
  }
  
  return true;
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // TODO Auto-generated method stub
  super.onCreateOptionsMenu(menu);
  
  menu.add(0, ZOOM_IN, Menu.NONE, "放大");
  menu.add(0, ZOOM_OUT, Menu.NONE, "縮小");
  return true;
 }
 
 class MyLocationOverlay extends Overlay{
  Location mLocation;
  //更新坐標時,設置該坐標,以便畫圖
  public void setLocation(Location location){
   mLocation = location;
  }
  
  @Override
  public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
    long when) {
   // TODO Auto-generated method stub
   super.draw(canvas, mapView, shadow, when);
   
   Paint paint = new Paint();
   Point myScreenCoords = new Point();
   //將經緯度轉換成實際屏幕坐標
   GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude() * 1E6), (int)(mLocation.getLongitude() * 1E6));
   mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
   paint.setStrokeWidth(1);
   paint.setARGB(255, 255, 0, 0);
   paint.setStyle(Paint.Style.STROKE);
   Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home);
   canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
   canvas.drawText("Here am I", myScreenCoords.x, myScreenCoords.y, paint);
   
   return true;
  }  
 }
}

 

 

運行效果:

 

 

註意:Loction常常獲取null,在網上查瞭很多資料。發現最主要是我們不能查到那個GPS提供商的能提供定位,有用while循環知道獲取停止,但是這個時間可能等待很長的時間都不能獲取到,我是采用下面的

 

[java] String provider = locationManager.getBestProvider(criteria, true); 
        List<String> privatelist= locationManager.getAllProviders(); 
        for(String privates:privatelist) 
        { 
            Location locat=locationManager.getLastKnownLocation(privates); 
            if(locat!=null) 
            { 
                provider=privates; 
                break; 
            } 
        } 
        //獲得坐標相應信息  
        Location location = locationManager.getLastKnownLocation(provider); 
String provider = locationManager.getBestProvider(criteria, true);
  List<String> privatelist= locationManager.getAllProviders();
  for(String privates:privatelist)
  {
   Location locat=locationManager.getLastKnownLocation(privates);
   if(locat!=null)
   {
    provider=privates;
    break;
   }
  }
  //獲得坐標相應信息
  Location location = locationManager.getLastKnownLocation(provider);

 

 

這樣可以檢測到,但是這個不是最優的方法,但是可以得到運行的效果。

 

摘自 北京大學-Google Android實驗室

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *