Android-百度地图定位实现仿钉钉签到打卡功能(附源码)

/***

  • 接收定位结果消息,并显示在地图上
    */
    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 longItems = new ArrayList(); //经度集合
    List latItems = new ArrayList(); //纬度集合

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

【附】相关架构及资料

image

资料领取

关注+点赞+加群:185873940 免费获取!

总结

最后对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
中…(img-skn2Jo6Q-1714701949064)]

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值