免费做二维码网站,Wordpress主题更改导航栏颜色,旅游网站如何做推广,防止服务器上的网站被进攻新功能添加#xff1a;第一#xff0c;定位功能#xff1b;第二#xff0c;與方向傳感器結合#xff0c;通過旋轉手機進行道路的方向確認。1、初次啟動定位/*** 定位的客戶端*/privateLocationClient mLocationClient;/*** 定位的監聽器*/publicMyLocationListener mMyLoc…新功能添加第一定位功能第二與方向傳感器結合通過旋轉手機進行道路的方向確認。1、初次啟動定位/*** 定位的客戶端*/privateLocationClient mLocationClient;/*** 定位的監聽器*/publicMyLocationListener mMyLocationListener;/*** 當前定位的模式*/privateLocationMode mCurrentMode LocationMode.NORMAL;/**** 是否是第一次定位*/privatevolatilebooleanisFristLocation true;/*** 初始化定位相關代碼*/privatevoidinitMyLocation(){// 定位初始化mLocationClient newLocationClient(this);mMyLocationListener newMyLocationListener();mLocationClient.registerLocationListener(mMyLocationListener);// 設置定位的相關配置LocationClientOption option newLocationClientOption();option.setOpenGps(true);// 打開gpsoption.setCoorType(bd09ll);// 設置坐標類型option.setScanSpan(1000);mLocationClient.setLocOption(option);}然后是定位的監聽器MyLocationListener/*** 實現實位回調監聽*/publicclassMyLocationListenerimplementsBDLocationListener{OverridepublicvoidonReceiveLocation(BDLocation location){// map view 銷毀后不在處理新接收的位置if(location null|| mMapView null)return;// 構造定位數據MyLocationData locData newMyLocationData.Builder().accuracy(location.getRadius())// 此處設置開發者獲取到的方向信息順時針0-360.direction(mXDirection).latitude(location.getLatitude()).longitude(location.getLongitude()).build();mCurrentAccracy location.getRadius();// 設置定位數據mBaiduMap.setMyLocationData(locData);mCurrentLantitude location.getLatitude();mCurrentLongitude location.getLongitude();// 設置自定義圖標BitmapDescriptor mCurrentMarker BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked);MyLocationConfigeration config newMyLocationConfigeration(mCurrentMode, true, mCurrentMarker);mBaiduMap.setMyLocationConfigeration(config);// 第一次定位時將地圖位置移動到當前位置if(isFristLocation){isFristLocation false;LatLng ll newLatLng(location.getLatitude(),location.getLongitude());MapStatusUpdate u MapStatusUpdateFactory.newLatLng(ll);mBaiduMap.animateMapStatus(u);}}}可以看到我們初始化了定位的參數設置了定位的監聽器每隔1s會進行一次定位應用打開時第一定位會把地圖中心設置當前用戶位置。定位也是比較耗電的所以我們在onStart中開啟定位在onStop中關閉定位~~這樣應用最小化時就不會一直在哪GPS請求定位了用戶要是看你app一直在那定位估計馬上就被卸載了~OverrideprotectedvoidonStart(){// 開啟圖層定位mBaiduMap.setMyLocationEnabled(true);if(!mLocationClient.isStarted()){mLocationClient.start();}// 開啟方向傳感器myOrientationListener.start();super.onStart();}OverrideprotectedvoidonStop(){// 關閉圖層定位mBaiduMap.setMyLocationEnabled(false);mLocationClient.stop();// 關閉方向傳感器myOrientationListener.stop();super.onStop();}上面的傳感器的代碼一會就會介紹~記得在AndroidManifest.xml配一個serviceandroid:namecom.baidu.location.fandroid:enabledtrueandroid:process:remote現在基本的定位功能已經實現了~不過我們還需要添加點擊定位按鈕和方向傳感器2、我的位置點擊我的位置菜單會調用center2myLoc方法。caseR.id.id_menu_map_myLoc:center2myLoc();break;/*** 地圖移動到我的位置,此處可以重新發定位請求然后定位* 直接拿最近一次經緯度如果長時間沒有定位成功可能會顯示效果不好*/privatevoidcenter2myLoc(){LatLng ll newLatLng(mCurrentLantitude, mCurrentLongitude);MapStatusUpdate u MapStatusUpdateFactory.newLatLng(ll);mBaiduMap.animateMapStatus(u);}很簡單我們在定位的監聽器中已經保存了最近一次的定位經緯度所以只需要點擊時把地圖移動到相應的位置即可。3、集成方向傳感器首先是封裝的方向傳感器的類MyOrientationListener.javapackagecom.zhy.zhy_baidu_ditu_demo00;importandroid.content.Context;importandroid.hardware.Sensor;importandroid.hardware.SensorEvent;importandroid.hardware.SensorEventListener;importandroid.hardware.SensorManager;publicclassMyOrientationListenerimplementsSensorEventListener{privateContext context;privateSensorManager sensorManager;privateSensor sensor;privatefloatlastX ;privateOnOrientationListener onOrientationListener ;publicMyOrientationListener(Context context){this.context context;}// 開始publicvoidstart(){// 獲得傳感器管理器sensorManager (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);if(sensorManager !null){// 獲得方向傳感器sensor sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);}// 注冊if(sensor !null){//SensorManager.SENSOR_DELAY_UIsensorManager.registerListener(this, sensor,SensorManager.SENSOR_DELAY_UI);}}// 停止檢測publicvoidstop(){sensorManager.unregisterListener(this);}OverridepublicvoidonAccuracyChanged(Sensor sensor,intaccuracy){}OverridepublicvoidonSensorChanged(SensorEvent event){// 接受方向感應器的類型if(event.sensor.getType() Sensor.TYPE_ORIENTATION){// 這里我們可以得到數據然后根據需要來處理floatx event.values[SensorManager.DATA_X];if( Math.abs(x- lastX) 1.0){onOrientationListener.onOrientationChanged(x);}// Log.e(DATA_X, x);lastX x ;}}publicvoidsetOnOrientationListener(OnOrientationListener onOrientationListener){this.onOrientationListener onOrientationListener ;}publicinterfaceOnOrientationListener{voidonOrientationChanged(floatx);}}在onCreate中初始化方向傳感器/*** 初始化方向傳感器*/privatevoidinitOritationListener(){myOrientationListener newMyOrientationListener(getApplicationContext());myOrientationListener.setOnOrientationListener(newOnOrientationListener(){OverridepublicvoidonOrientationChanged(floatx){mXDirection (int) x;// 構造定位數據MyLocationData locData newMyLocationData.Builder().accuracy(mCurrentAccracy)// 此處設置開發者獲取到的方向信息順時針0-360.direction(mXDirection).latitude(mCurrentLantitude).longitude(mCurrentLongitude).build();// 設置定位數據mBaiduMap.setMyLocationData(locData);// 設置自定義圖標BitmapDescriptor mCurrentMarker BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked);MyLocationConfigeration config newMyLocationConfigeration(mCurrentMode, true, mCurrentMarker);mBaiduMap.setMyLocationConfigeration(config);}});}最后在onStart和onStop中分別開啟和關閉方向傳感器。對於旋轉手機確定方向實際上利用了newMyLocationData.Builder()//此處設置開發者獲取到的方向信息順時針0-360 .direction(mXDirection)只需要把x方向的角度設置即可