導語
本章根據百度地圖API,實作仿釘釘打卡功能。用到了基礎地圖、覆寫物、定位圖層、陀螺儀方法、懸浮資訊彈框。
百度地圖API位址 :Android 地圖SDK
請先注冊注冊百度賬号和擷取密鑰,并實作地圖顯示出來。(注意:密鑰、權限要設定)
另外,我得說明本章所下載下傳官方Demo 和 導入的jar包和so檔案。自定義下載下傳即可,如下圖:
![]()
Android 百度地圖定位實作仿釘釘簽到打卡功能(附源碼)導語 接下來,一起看實作效果。
源碼Git位址:BaiduMapApp
效果圖
實作代碼·三步驟
第一步:基礎地圖和方向傳感器
類先實作方向傳感器 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()
// 此處設定開發者擷取到的方向資訊,順時針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);//擷取傳感器管理服務
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
//為系統的方向傳感器注冊監聽器
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();
//取消注冊傳感器監聽
mSensorManager.unregisterListener(this);
}
第二步:開啟定位
/***
* 定位選項設定
* @return
*/
public void getLocationClientOption() {
mOption = new LocationClientOption();
mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可選,預設高精度,設定定位模式,高精度,低功耗,僅裝置
mOption.setCoorType("bd09ll");//可選,預設gcj02,設定傳回的定位結果坐标系,如果配合百度地圖使用,建議設定為bd09ll;
mOption.setScanSpan(2000);//可選,預設0,即僅定位一次,設定發起連續定位請求的間隔需要大于等于1000ms才是有效的
mOption.setIsNeedAddress(true);//可選,設定是否需要位址資訊,預設不需要
mOption.setIsNeedLocationDescribe(true);//可選,設定是否需要位址描述
mOption.setNeedDeviceDirect(true);//可選,設定是否需要裝置方向結果
mOption.setLocationNotify(true);//可選,預設false,設定是否當gps有效時按照1S1次頻率輸出GPS結果
mOption.setIgnoreKillProcess(true);//可選,預設true,定位SDK内部是一個SERVICE,并放到了獨立程序,設定是否在stop的時候殺死這個程序,預設不殺死
mOption.setIsNeedLocationDescribe(false);//可選,預設false,設定是否需要位置語義化結果,可以在BDLocation.getLocationDescribe裡得到,結果類似于“在北京天安門附近”
mOption.setIsNeedLocationPoiList(false);//可選,預設false,設定是否需要POI結果,可以在BDLocation.getPoiList裡得到
mOption.SetIgnoreCacheException(false);//可選,預設false,設定是否收集CRASH資訊,預設收集
mOption.setOpenGps(true);//可選,預設false,設定是否開啟Gps定位
mOption.setIsNeedAltitude(false);//可選,預設false,設定定位時是否需要海拔資訊,預設不需要,除基礎定位版本都可用
client = new LocationClient(this);
client.setLocOption(mOption);
client.registerLocationListener(BDAblistener);
client.start();
}
/***
* 接收定位結果消息,并顯示在地圖上
*/
private BDAbstractLocationListener BDAblistener = new BDAbstractLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
//定位方向
mCurrentLat = location.getLatitude();
mCurrentLon = location.getLongitude();
//個人定位
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
//設定打卡目标範圍圈
private void setCircleOptions() {
if (mDestinationPoint == null) return;//打卡坐标不能為空
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);
}
/**
* 設定marker覆寫物
*
* @param ll 坐标
* @param icon 圖示
*/
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) {//打卡坐标不為空
mZoomScale = getZoomScale(ll);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(ll, mZoomScale));//縮放
} else {
mZoomScale = getZoomScale(ll);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngZoom(mCenterPos, mZoomScale));//縮放
}
}
/**
* 擷取地圖的中心點和縮放比例
*
* @return float
*/
private float getZoomScale(LatLng LocationPoint) {
double maxLong; //最大經度
double minLong; //最小經度
double maxLat; //最大緯度
double minLat; //最小緯度
List<Double> longItems = new ArrayList<Double>(); //經度集合
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); //最大經度
minLong = longItems.get(0); //最小經度
maxLat = latItems.get(0); //最大緯度
minLat = latItems.get(0); //最小緯度
for (int i = 0; i < longItems.size(); i++) {
maxLong = Math.max(maxLong, longItems.get(i)); //擷取集合中的最大經度
minLong = Math.min(minLong, longItems.get(i)); //擷取集合中的最小經度
}
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));//縮放比例參數
mCenterPos = new LatLng(latCenter, longCenter); //擷取中心點經緯度
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;
}
/**
* 縮放比例參數
*
* @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;
}
}
/**
* 處理連續定位的地圖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);//假設公司坐标
setCircleOptions();
//計算兩點距離,機關:米
mDistance = DistanceUtil.getDistance(mDestinationPoint, LocationPoint);
if (mDistance <= DISTANCE) {
//顯示文字
setTextOption(mDestinationPoint, "您已在餐廳範圍内", "#7ED321");
//目的地圖示
setMarkerOptions(mDestinationPoint, R.mipmap.arrive_icon);
//按鈕顔色
//commit_bt.setBackgroundDrawable(getResources().getDrawable(R.mipmap.restaurant_btbg_yellow));
mBaiduMap.setMyLocationEnabled(false);
} else {
setTextOption(LocationPoint, "您不在餐廳範圍之内", "#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);
}
};
實作時間顯示
/**
* 設定系統時間
*/
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());//擷取目前時間
mTime_tv.setText(simpleDateFormat.format(date)); //更新時間
mHandler.postDelayed(run, 1000);
}
};
mHandler.post(run);//設定系統時間
最後,收尾操作
@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
歡迎入坑。