标签:
android提供了LocationManager来取得位置,用LocationListener来监听位置的变化
先做一些初始化工作:
/** latitude and longitude of current location*/ public static String mLat = ""; public static String mLon = ""; /** time out for GPS location update */ private Timer mGpsTimer = new Timer(); /** TimerTask for time out of GPS location update */ private GpsTimeOutTask mGpsTimeOutTask = new GpsTimeOutTask(); /** GPS location update time out in milliseconds*/ private long mGpsTimeOut = 180000;//3 minutes <span style="white-space:pre"> </span>public void initiLocationUtil (Context context, LocationObsever locationobsever){ mLocationObsever = locationobsever; mContext = context; mLocationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE); mLocationListener = new MyLocationListener(); }
<span style="white-space:pre"> </span>public void RefreshGPS(boolean calledByCreate){ mLocationManager.removeUpdates(mLocationListener); boolean providerEnable = true; boolean showLocationServiceDisableNotice = true; //看是否有GPS权限 if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ //開始进行定位 mLocationListener为位置监听器 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener); showLocationServiceDisableNotice = false; //start time out timer mGpsTimer = new Timer(); mGpsTimeOutTask = new GpsTimeOutTask(); mGpsTimer.schedule(mGpsTimeOutTask, mGpsTimeOut); } if(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener); showLocationServiceDisableNotice = false; providerEnable = true; } if(providerEnable){ if(mLocationObsever != null){ mLocationObsever.notifyChange(REFRESHGPS_COMPLETED, null); } }else{ if(mLocationObsever != null){ mLocationObsever.notifyChange(REFRESHGPS_NOPROVIDER, null); } } if(showLocationServiceDisableNotice){ showLocationServiceDisabledDialog(); } }
监听器:
private class MyLocationListener implements LocationListener{ private boolean mLocationReceived = false; @Override public void onLocationChanged(Location location) { if(location != null && !mLocationReceived){ mLocationReceived = true; String lon = String.valueOf(location.getLongitude()); String lat = String.valueOf(location.getLatitude()); if(mLocationObsever != null){ mLocationObsever.notifyChange(DEFAULT_LOCATION_COMPLETED, lat+","+lon); } }else if(location == null){ if(mLocationObsever != null){ mLocationObsever.notifyChange(GETLOCATION_FAILED, null); } } } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub //if GPS provider is not accessible, try network provider if(provider.equals(LocationManager.GPS_PROVIDER) && status != LocationProvider.AVAILABLE){ if(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener); }else{ mLocationManager.removeUpdates(mLocationListener); if(mLocationObsever != null){ mLocationObsever.notifyChange(STATUS_CHANGED, null); } } } } }
这里用了一个Timer,3分钟后又一次去取一次位置:
private Handler mGpsTimerHandler = new Handler() { public void handleMessage(Message msg) { if (mLocationManager == null) { return; } if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { System.out.println("=====use network to get location"); mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener); } else { mLocationManager.removeUpdates(mLocationListener); // mLocationObsever.notifyChange(SETADDLOCATIONBUTTONSTATE_1_SETLOCATIONDES_1,null); if (mLocationObsever != null) { mLocationObsever.notifyChange(GPSTIMEOUT, null); } } } };
界面退出的时候要关掉GPS
/** * cancel operations of refreshing GPS */ public void cancelRefreshGPS(){ if(mLocationManager != null){ mLocationManager.removeUpdates(mLocationListener); } if(mLocationObsever != null){ mLocationObsever.notifyChange(CANCELGPS_COMPLETED, null); } } public void destroy (){ if(mLocationManager != null){ mLocationManager.removeUpdates(mLocationListener); } if(mGpsTimer != null){ mGpsTimer.cancel(); } cancelRefreshGPS(); mContext = null; mLocationObsever = null; mLocationBuildingList = null; System.gc(); }
点击MAP的时候,假设採用google map,必须使用sdk带有google api,然后在application中增加<uses-library android:name="com.google.android.maps" />
然后xml是:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF" android:gravity="center_horizontal" android:orientation="vertical" > <include layout="@layout/title_list" /> <View android:id="@+id/line" android:layout_width="fill_parent" android:layout_height="2dip" android:layout_below="@id/title" android:background="@drawable/rc_list_divider" /> <com.google.android.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="06nx-Rzpy8WU16_gjO8ZbtRYYY-junnxNArrxFg" /> </LinearLayout>
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.around_map); mTextView = (TextView)findViewById(R.id.title_text); mTextView.setText("地图"); Intent intent = getIntent(); mLatitude = intent.getDoubleExtra("lat", 0.0); mLongitude = intent.getDoubleExtra("lon", 0.0); mMapView = (MapView) findViewById(R.id.mapview); mMapView.setClickable(true); mMapView.setBuiltInZoomControls(true); mMapView.setSatellite(true); mapController = mMapView.getController(); // geoPoint = new GeoPoint((int)(mLatitude * 1E6), (int)(mLongitude * 1E6)); geoPoint=new GeoPoint((int)(30.659259*1000000),(int)(104.065762*1000000)); mMapView.displayZoomControls(true); // // 设置地图的初始大小。范围在1和21之间。1:最小尺寸,21:最大尺寸 mapController.setZoom(16); // // // 创建MyOverlay对象,用于在地图上绘制图形 MyOverlay myOverlay = new MyOverlay(); mMapView.getOverlays().add(myOverlay); mapController.animateTo(geoPoint); } @Override protected boolean isRouteDisplayed() { return false; } class MyOverlay extends Overlay { @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { Paint paint = new Paint(); //屏幕上文字字体颜色 paint.setColor(Color.RED); Point screenPoint = new Point(); mapView.getProjection().toPixels(geoPoint, screenPoint); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.location); // 在地图上绘制图像 canvas.drawBitmap(bmp, screenPoint.x, screenPoint.y, paint); // 在地图上绘制文字 // canvas.drawText("移动巴士", 10, 100, paint); return super.draw(canvas, mapView, shadow, when); } }
代码能够在http://download.csdn.net/detail/baidu_nod/7622677下载
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/5179379.html