亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能的完整代碼

 更新時(shí)間:2020年04月09日 11:42:34   作者:艾陽(yáng)丶  
這篇文章主要介紹了Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

導(dǎo)語(yǔ)

本章根據(jù)百度地圖API,實(shí)現(xiàn)仿釘釘打卡功能。用到了基礎(chǔ)地圖、覆蓋物、定位圖層、陀螺儀方法、懸浮信息彈框。

百度地圖API地址  :Android 地圖SDK

請(qǐng)先注冊(cè)注冊(cè)百度賬號(hào)和獲取密鑰,并實(shí)現(xiàn)地圖顯示出來(lái)。(注意:密鑰、權(quán)限要設(shè)置)

另外,我得說(shuō)明本章所下載官方Demo 和 導(dǎo)入的jar包和so文件。自定義下載即可,如下圖:

接下來(lái),一起看實(shí)現(xiàn)效果。

源碼Git地址:BaiduMapApp

效果圖        

   

實(shí)現(xiàn)代碼·三步驟

第一步:基礎(chǔ)地圖和方向傳感器

類先實(shí)現(xiàn)方向傳感器 implements SensorEventListener

@Override
 public void onSensorChanged(SensorEvent sensorEvent) {
  double x = sensorEvent.values[SensorManager.DATA_X];
  if (Math.abs(x - lastX) > 1.0) {
   mCurrentDirection = (int) x;
   locData = new MyLocationData.Builder()
     // 此處設(shè)置開發(fā)者獲取到的方向信息,順時(shí)針0-360
     .direction(mCurrentDirection).latitude(mCurrentLat)
     .longitude(mCurrentLon).build();
   mBaiduMap.setMyLocationData(locData);
  }
  lastX = x;
 }
 
 @Override
 public void onAccuracyChanged(Sensor sensor, int i) {
 
 }
 /**
  * 初始化地圖
  */
 private void initBaiduMap() {
  mMapView = (MapView) findViewById(R.id.mapview);
  mBaiduMap = mMapView.getMap();
  mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
  mBaiduMap.setMyLocationEnabled(true);//開啟定位圖層
  mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);//獲取傳感器管理服務(wù)
 }
@Override
 protected void onResume() {
  super.onResume();
  mMapView.onResume();
  //為系統(tǒng)的方向傳感器注冊(cè)監(jiān)聽器
  mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
    SensorManager.SENSOR_DELAY_UI);
 }
 @Override
 protected void onPause() {
  super.onPause();
  mMapView.onPause();
 }
 
 @Override
 protected void onStop() {
  super.onStop();
  //取消注冊(cè)傳感器監(jiān)聽
  mSensorManager.unregisterListener(this);
 }

第二步:開啟定位

 /***
  * 定位選項(xiàng)設(shè)置
  * @return
  */
 public void getLocationClientOption() {
  mOption = new LocationClientOption();
  mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可選,默認(rèn)高精度,設(shè)置定位模式,高精度,低功耗,僅設(shè)備
  mOption.setCoorType("bd09ll");//可選,默認(rèn)gcj02,設(shè)置返回的定位結(jié)果坐標(biāo)系,如果配合百度地圖使用,建議設(shè)置為bd09ll;
  mOption.setScanSpan(2000);//可選,默認(rèn)0,即僅定位一次,設(shè)置發(fā)起連續(xù)定位請(qǐng)求的間隔需要大于等于1000ms才是有效的
  mOption.setIsNeedAddress(true);//可選,設(shè)置是否需要地址信息,默認(rèn)不需要
  mOption.setIsNeedLocationDescribe(true);//可選,設(shè)置是否需要地址描述
  mOption.setNeedDeviceDirect(true);//可選,設(shè)置是否需要設(shè)備方向結(jié)果
  mOption.setLocationNotify(true);//可選,默認(rèn)false,設(shè)置是否當(dāng)gps有效時(shí)按照1S1次頻率輸出GPS結(jié)果
  mOption.setIgnoreKillProcess(true);//可選,默認(rèn)true,定位SDK內(nèi)部是一個(gè)SERVICE,并放到了獨(dú)立進(jìn)程,設(shè)置是否在stop的時(shí)候殺死這個(gè)進(jìn)程,默認(rèn)不殺死
  mOption.setIsNeedLocationDescribe(false);//可選,默認(rèn)false,設(shè)置是否需要位置語(yǔ)義化結(jié)果,可以在BDLocation.getLocationDescribe里得到,結(jié)果類似于“在北京天安門附近”
  mOption.setIsNeedLocationPoiList(false);//可選,默認(rèn)false,設(shè)置是否需要POI結(jié)果,可以在BDLocation.getPoiList里得到
  mOption.SetIgnoreCacheException(false);//可選,默認(rèn)false,設(shè)置是否收集CRASH信息,默認(rèn)收集
  mOption.setOpenGps(true);//可選,默認(rèn)false,設(shè)置是否開啟Gps定位
  mOption.setIsNeedAltitude(false);//可選,默認(rèn)false,設(shè)置定位時(shí)是否需要海拔信息,默認(rèn)不需要,除基礎(chǔ)定位版本都可用
  client = new LocationClient(this);
  client.setLocOption(mOption);
  client.registerLocationListener(BDAblistener);
  client.start();
 }
 /***
  * 接收定位結(jié)果消息,并顯示在地圖上
  */
 private BDAbstractLocationListener BDAblistener = new BDAbstractLocationListener() {
  @Override
  public void onReceiveLocation(BDLocation location) {
   //定位方向
   mCurrentLat = location.getLatitude();
   mCurrentLon = location.getLongitude();
   //個(gè)人定位
   locData = new MyLocationData.Builder()
     .direction(mCurrentDirection).latitude(location.getLatitude())
     .longitude(location.getLongitude()).build();
   mBaiduMap.setMyLocationData(locData);
   mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
     MyLocationConfiguration.LocationMode.NORMAL, true, null));
   //更改UI
   Message message = new Message();
   message.obj = location;
   mHandler.sendMessage(message);
  }
 };

第三步:更改UI

 //設(shè)置打卡目標(biāo)范圍圈
 private void setCircleOptions() {
  if (mDestinationPoint == null) return;//打卡坐標(biāo)不能為空
  OverlayOptions ooCircle = new CircleOptions().fillColor(0x4057FFF8)
    .center(mDestinationPoint).stroke(new Stroke(1, 0xB6FFFFFF)).radius(DISTANCE);
  mBaiduMap.addOverlay(ooCircle);
 }
 /**
  * 添加地圖文字
  *
  * @param point
  * @param str
  * @param color 字體顏色
  */
 private void setTextOption(LatLng point, String str, String color) {
  //使用MakerInfoWindow
  if (point == null) return;
  TextView view = new TextView(getApplicationContext());
  view.setBackgroundResource(R.mipmap.map_textbg);
  view.setPadding(0, 23, 0, 0);
  view.setTypeface(Typeface.DEFAULT_BOLD);
  view.setTextSize(14);
  view.setGravity(Gravity.CENTER);
  view.setText(str);
  view.setTextColor(Color.parseColor(color));
  mInfoWindow = new InfoWindow(view, point, 170);
  mBaiduMap.showInfoWindow(mInfoWindow);
 }
 /**
  * 設(shè)置marker覆蓋物
  *
  * @param ll 坐標(biāo)
  * @param icon 圖標(biāo)
  */
 private void setMarkerOptions(LatLng ll, int icon) {
  if (ll == null) return;
  BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(icon);
  MarkerOptions ooD = new MarkerOptions().position(ll).icon(bitmap);
  mBaiduMap.addOverlay(ooD);
 }
 //改變地圖縮放
 private void setMapZoomScale(LatLng ll) {
  if (mDestinationPoint == null) {//打卡坐標(biāo)不為空
   mZoomScale = getZoomScale(ll);
   mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(ll, mZoomScale));//縮放
  } else {
   mZoomScale = getZoomScale(ll);
   mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(mCenterPos, mZoomScale));//縮放
  }
 }
 /**
  * 獲取地圖的中心點(diǎn)和縮放比例
  *
  * @return float
  */
 private float getZoomScale(LatLng LocationPoint) {
  double maxLong; //最大經(jīng)度
  double minLong; //最小經(jīng)度
  double maxLat;  //最大緯度
  double minLat;  //最小緯度
  List<Double> longItems = new ArrayList<Double>(); //經(jīng)度集合
  List<Double> latItems = new ArrayList<Double>();  //緯度集合
 
  if (null != LocationPoint) {
   longItems.add(LocationPoint.longitude);
   latItems.add(LocationPoint.latitude);
  }
  if (null != mDestinationPoint) {
   longItems.add(mDestinationPoint.longitude);
   latItems.add(mDestinationPoint.latitude);
  }
 
  maxLong = longItems.get(0); //最大經(jīng)度
  minLong = longItems.get(0); //最小經(jīng)度
  maxLat = latItems.get(0);  //最大緯度
  minLat = latItems.get(0);  //最小緯度
 
  for (int i = 0; i < longItems.size(); i++) {
   maxLong = Math.max(maxLong, longItems.get(i)); //獲取集合中的最大經(jīng)度
   minLong = Math.min(minLong, longItems.get(i)); //獲取集合中的最小經(jīng)度
  }
 
  for (int i = 0; i < latItems.size(); i++) {
   maxLat = Math.max(maxLat, latItems.get(i)); //獲取集合中的最大緯度
   minLat = Math.min(minLat, latItems.get(i)); //獲取集合中的最小緯度
  }
  double latCenter = (maxLat + minLat) / 2;
  double longCenter = (maxLong + minLong) / 2;
  int jl = (int) getDistance(new LatLng(maxLat, maxLong), new LatLng(minLat, minLong));//縮放比例參數(shù)
  mCenterPos = new LatLng(latCenter, longCenter); //獲取中心點(diǎn)經(jīng)緯度
  int zoomLevel[] = {2500000, 2000000, 1000000, 500000, 200000, 100000,
    50000, 25000, 20000, 10000, 5000, 2000, 1000, 500, 100, 50, 20, 0};
  int i;
  for (i = 0; i < 18; i++) {
   if (zoomLevel[i] < jl) {
    break;
   }
  }
  float zoom = i + 4;
  return zoom;
 }
 
 /**
  * 縮放比例參數(shù)
  *
  * @param var0
  * @param var1
  * @return
  */
 public double getDistance(LatLng var0, LatLng var1) {
  if (var0 != null && var1 != null) {
   Point var2 = CoordUtil.ll2point(var0);
   Point var3 = CoordUtil.ll2point(var1);
   return var2 != null && var3 != null ? CoordUtil.getDistance(var2, var3) : -1.0D;
  } else {
   return -1.0D;
  }
 }
 /**
  * 處理連續(xù)定位的地圖UI變化
  */
 private Handler mHandler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   BDLocation location = (BDLocation) msg.obj;
   LatLng LocationPoint = new LatLng(location.getLatitude(), location.getLongitude());
   //打卡范圍
   mDestinationPoint = new LatLng(location.getLatitude() * 1.0001, location.getLongitude() * 1.0001);//假設(shè)公司坐標(biāo)
   setCircleOptions();
   //計(jì)算兩點(diǎn)距離,單位:米
   mDistance = DistanceUtil.getDistance(mDestinationPoint, LocationPoint);
   if (mDistance <= DISTANCE) {
    //顯示文字
    setTextOption(mDestinationPoint, "您已在餐廳范圍內(nèi)", "#7ED321");
    //目的地圖標(biāo)
    setMarkerOptions(mDestinationPoint, R.mipmap.arrive_icon);
    //按鈕顏色
    //commit_bt.setBackgroundDrawable(getResources().getDrawable(R.mipmap.restaurant_btbg_yellow));
    mBaiduMap.setMyLocationEnabled(false);
   } else {
    setTextOption(LocationPoint, "您不在餐廳范圍之內(nèi)", "#FF6C6C");
    setMarkerOptions(mDestinationPoint, R.mipmap.restaurant_icon);
    //commit_bt.setBackgroundDrawable(getResources().getDrawable(R.mipmap.restaurant_btbg_gray));
    mBaiduMap.setMyLocationEnabled(true);
   }
   // mDistance_tv.setText("距離目的地:" + mDistance + "米");
   //縮放地圖
   setMapZoomScale(LocationPoint);
  }
 };

實(shí)現(xiàn)時(shí)間顯示

 /**
  * 設(shè)置系統(tǒng)時(shí)間
  */
 private Runnable run = new Runnable() {
  @Override
  public void run() {
   SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");// HH:mm:ss
   Date date = new Date(System.currentTimeMillis());//獲取當(dāng)前時(shí)間
   mTime_tv.setText(simpleDateFormat.format(date)); //更新時(shí)間
   mHandler.postDelayed(run, 1000);
  }
 };
  mHandler.post(run);//設(shè)置系統(tǒng)時(shí)間

最后,收尾操作

 @Override
 protected void onDestroy() {
  if (BDAblistener != null) {
   client.unRegisterLocationListener(BDAblistener);
 
  }
  if (client != null && client.isStarted()) {
   client.stop();
  }
  mMapView.onDestroy();
  mMapView = null;
  mHandler.removeCallbacks(run);
  super.onDestroy();
 }

源碼地址:https://github.com/aiyangtianci/BaiduMapApp

總結(jié)

到此這篇關(guān)于Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能的文章就介紹到這了,更多相關(guān)android 釘釘簽到打卡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android SeekBar控制視頻播放進(jìn)度實(shí)現(xiàn)過(guò)程講解

    Android SeekBar控制視頻播放進(jìn)度實(shí)現(xiàn)過(guò)程講解

    這篇文章主要介紹了Android SeekBar控制視頻播放進(jìn)度實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-04-04
  • 基于Android實(shí)現(xiàn)仿QQ5.0側(cè)滑

    基于Android實(shí)現(xiàn)仿QQ5.0側(cè)滑

    本課程將帶領(lǐng)大家通過(guò)自定義控件實(shí)現(xiàn)QQ5.0側(cè)滑菜單,課程將循序漸進(jìn),首先實(shí)現(xiàn)最普通的側(cè)滑菜單,然后引入屬性動(dòng)畫與拖動(dòng)菜單效果相結(jié)合,最終實(shí)現(xiàn)QQ5.0側(cè)滑菜單效果。通過(guò)本課程大家會(huì)對(duì)側(cè)滑菜單有更深層次的了解,通過(guò)自定義控件和屬性動(dòng)畫打造千變?nèi)f化的側(cè)滑菜單效果
    2015-12-12
  • Android實(shí)現(xiàn)底部彈出按鈕菜單升級(jí)版

    Android實(shí)現(xiàn)底部彈出按鈕菜單升級(jí)版

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部彈出按鈕菜單的升級(jí)版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 判斷Android程序是否在前臺(tái)運(yùn)行的兩種方法

    判斷Android程序是否在前臺(tái)運(yùn)行的兩種方法

    這篇文章主要介紹了判斷Android程序是否在前臺(tái)運(yùn)行的兩種方法,本文直接給出實(shí)現(xiàn)代碼,,需要的朋友可以參考下
    2015-06-06
  • Android View的事件分發(fā)機(jī)制深入分析講解

    Android View的事件分發(fā)機(jī)制深入分析講解

    事件分發(fā)從手指觸摸屏幕開始,即產(chǎn)生了觸摸信息,被底層系統(tǒng)捕獲后會(huì)傳遞給Android的輸入系統(tǒng)服務(wù)IMS,通過(guò)Binder把消息發(fā)送到activity,activity會(huì)通過(guò)phoneWindow、DecorView最終發(fā)送給ViewGroup。這里就直接分析ViewGroup的事件分發(fā)
    2023-01-01
  • 學(xué)習(xí)使用Material Design控件(一)

    學(xué)習(xí)使用Material Design控件(一)

    這篇文章主要為大家介紹了學(xué)習(xí)使用Material Design控件的詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android Studio多渠道批量打包及代碼混淆

    Android Studio多渠道批量打包及代碼混淆

    這篇文章主要介紹了Android Studio多渠道批量打包及代碼混淆的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • Android Bitmap和Drawable相互轉(zhuǎn)換的簡(jiǎn)單代碼

    Android Bitmap和Drawable相互轉(zhuǎn)換的簡(jiǎn)單代碼

    Android Bitmap和Drawable相互轉(zhuǎn)換的簡(jiǎn)單代碼,需要的朋友可以參考一下
    2013-05-05
  • Android WebView 優(yōu)化之路

    Android WebView 優(yōu)化之路

    Android WebView 優(yōu)化之路,如何才能更有效的對(duì)Android WebView進(jìn)行優(yōu)化,本文將為大家一一舉例,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android如何創(chuàng)建自定義ActionBar

    Android如何創(chuàng)建自定義ActionBar

    這篇文章主要教大家如何創(chuàng)建自定義的ActionBar,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11

最新評(píng)論