上一篇我們用了MyLocationOverlay,他會依照Wi-Fi或是GPS的定位資訊自行更新。但如果是我們自己想要監聽這些資訊,就必須利用LocationManager了。但首先我們必須要求使用者啟用相關功能才可以。
下面的程式碼是判斷使用者是否啟用了GPS定位,若是沒啟用,詢問是否需要開啟,使用者若選擇確定,則開啟設定介面:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){new AlertDialog.Builder(MeteorMap.this).setTitle("地圖工具").setMessage("您尚未開啟定位服務,要前往設定頁面啟動定位服務嗎?").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which){startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));}}).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which){Toast.makeText(MeteorMap.this, "未開啟定位服務,無法使用本工具!!", Toast.LENGTH_SHORT).show();}}).show();}
當確定啟用之後,接下來就是開啟監聽啦!這邊為了省事,所以直接利用原本的Activity實作LocationListener介面,然後註冊監聽事件:
//WI-FI定位locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, MeteorMap.this);//GPS 定位locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, MeteorMap.this);
註:平常我們開發通常在室內,所以GPS訊號應該是收不到,同時註冊網路定位可以方便測試,但實際上使用如果要求準確,就只需要註冊GPS定位!
若是要取消則只要:
locationManager.removeUpdates(MeteorMap.this);
接下來只要利用LocationListener的事件,我們可以就利用onLocationChanged事件來取得當前的定位資訊!
再來就要調整一下前一篇寫的程式,加上判斷以及註冊跟取消監聽,不過在那之前要先來瞭解一下Activity的生命週期:
當程式建立之後,若使用者跳離開畫面會進入onPause()事件(不管是關閉程式還是直接啟動別的程式),另外就是當使用者又回到程式畫面時都需要判斷是否啟用相關設定以及重新註冊事件,所以我覆寫原有的onResume以及onPause來處理這些設定。
以下是完整程式:
package demo.meteor.android.meteormap;import java.util.List;import com.google.android.maps.MapActivity;import com.google.android.maps.MapController;import com.google.android.maps.MapView;import com.google.android.maps.MyLocationOverlay;import com.google.android.maps.Overlay;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.provider.Settings;import android.util.Log;import android.widget.Toast;public class MeteorMap extends MapActivity implements LocationListener{private LocationManager locationManager;private MapView mapView;private MapController mapController;private MyLocationOverlay mylayer;private boolean enableTool;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);findControl();}private void init(){if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){new AlertDialog.Builder(MeteorMap.this).setTitle("地圖工具").setMessage("您尚未開啟定位服務,要前往設定頁面啟動定位服務嗎?").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which){startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));}}).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which){Toast.makeText(MeteorMap.this, "未開啟定位服務,無法使用本工具!!", Toast.LENGTH_SHORT).show();}}).show();}else{enableMyLocation();enableTool = true;}}private void findControl(){mapView = (MapView) findViewById(R.id.mapView);mapView.setBuiltInZoomControls(true);mapController = mapView.getController();mapController.setZoom(16);locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, MeteorMap.this);locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, MeteorMap.this);}private void enableMyLocation(){// 錨點List<Overlay> overlays = mapView.getOverlays();mylayer = new MyLocationOverlay(this, mapView);mylayer.enableCompass();mylayer.enableMyLocation();mylayer.runOnFirstFix(new Runnable(){public void run(){mapController.animateTo(mylayer.getMyLocation());}});overlays.add(mylayer);}@Overrideprotected void onResume(){super.onResume();if (enableTool){locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, MeteorMap.this);locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, MeteorMap.this);mylayer.enableMyLocation();mylayer.enableCompass();}else{init();}}@Overrideprotected void onPause(){super.onPause();if (enableTool){locationManager.removeUpdates(MeteorMap.this);mylayer.disableCompass();mylayer.disableMyLocation();}}@Overrideprotected boolean isRouteDisplayed(){// TODO Auto-generated method stubreturn false;}@Overridepublic void onLocationChanged(Location location){Log.v("map", location.toString());}@Overridepublic void onProviderDisabled(String provider){// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider){// TODO Auto-generated method stub}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras){// TODO Auto-generated method stub}}
因為這篇我只是把收到的定位資訊寫到訊息視窗,所以會看到的內容是:
Location[mProvider=network,mTime=1306399187835,mLatitude=24.9692934,mLongitude=121.23618125,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=60.0,mExtras=Bundle[mParcelledData.dataSize=148]]
後面就來拿這個玩囉!
原文來自:http://www.dotblogs.com.tw/alonstar/archive/2011/05/26/26303.aspx