电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android項目實戰之百度地圖地點簽到功能

瀏覽:157日期:2022-06-07 16:13:18

前言:先寫個簡單的地點簽到功能,如果日后有時間細寫的話,會更加好好研究一下百度地圖api,做更多邏輯判斷。

這里主要是調用百度地圖中的場景定位中的簽到場景。通過官方文檔進行api集成。通過GPS的定位功能,獲取地理位置,時間,用戶名進行存儲。之后通過日歷顯示歷史簽到記錄。

效果圖:

Android項目實戰之百度地圖地點簽到功能

Android項目實戰之百度地圖地點簽到功能 Android項目實戰之百度地圖地點簽到功能

/**百度地圖sdk**/ implementation files(’libs/BaiduLBS_Android.jar’) /**日歷選擇器**/ implementation ’com.prolificinteractive:material-calendarview:1.4.3’

簽到布局:

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:background='@color/color_ffffff' android:orientation='vertical' tools:context='.activity.SignInActivity'> <LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:layout_margin='20dp' android:orientation='vertical'> <TextView android: android:layout_width='match_parent' android:layout_height='wrap_content' android:textColor='@color/color_000000' android:textSize='18sp' /> <TextView android:layout_width='match_parent' android:layout_height='wrap_content' android:text='@string/check_in_area' /> <View /> <LinearLayout android: android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_margin='5dp' android:orientation='horizontal' android:visibility='gone'> <ImageView android:layout_width='40dp' android:layout_height='40dp' android:layout_margin='5dp' android:src='http://www.hdgsjgj.cn/bcjs/@mipmap/sign_in_address' /> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='vertical'> <TextView android: android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_margin='2dp' android:textColor='@color/color_000000' android:textSize='20sp' /> <LinearLayout android:layout_width='wrap_content' android:layout_height='wrap_content' android:orientation='horizontal'> <TextView android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_marginLeft='5dp' android:text='@string/sign_in_time' /> <TextView android: android:layout_width='match_parent' android:layout_height='wrap_content' /> </LinearLayout> <TextView android: android:layout_width='match_parent' android:layout_height='wrap_content' /> <Button android: android:layout_width='120dp' android:layout_height='40dp' android:layout_centerHorizontal='true' android:layout_marginTop='10dp' android:background='@drawable/btn_round_border' android:text='@string/sign_again' android:textAllCaps='false' android:textColor='@color/colorPrimary' android:textSize='15sp' /> </LinearLayout> </LinearLayout> <Button android:android:layout_gravity='center_vertical|center_horizontal' android:layout_marginTop='50dp' android:background='@drawable/btn_negative_nomal' android:text='@string/signIn' android:visibility='gone' /> </LinearLayout> </LinearLayout>

SignInActivity.java

public class SignInActivity extends BaseActivity { @BindView(R.id.sign_calendar) TextView signCalender; @BindView(R.id.line_sign_result) LinearLayout lineSignResult; @BindView(R.id.sign_in_result) TextView signInResult; @BindView(R.id.sign_in_time) TextView signInTime; @BindView(R.id.sign_address) TextView signAddress; @BindView(R.id.btn_sign_in) Button btnSignIn; private LocationService mLocationService; private boolean isAgain = false; SignIn signIn = new SignIn(); MyUser myUser = BmobUser.getCurrentUser(MyUser.class); @Override protected int contentViewID() { return R.layout.activity_sign_in; } @Override protected void initialize() { setTopTitle(getString(R.string.signIn), true); setLeftBtnFinish(); setDate(); setLocation(); querySignInState(); } /** * 查詢今日簽到狀態 */ private void querySignInState() { BmobQuery<SignIn> signInBmobQuery = new BmobQuery<SignIn>(); signInBmobQuery.addWhereEqualTo('username', myUser.getUsername()); signInBmobQuery.addWhereEqualTo('date', FormatUtils.getDateTimeString(Calendar.getInstance().getTime(), FormatUtils.template_Date)); signInBmobQuery.findObjects(new FindListener<SignIn>() { @Override public void done(List<SignIn> object, BmobException e) { if (e == null) { if (object.isEmpty()){ isAgain = false; btnSignIn.setVisibility(View.VISIBLE); } else { isAgain = true; SignIn signIn = object.get(0); btnSignIn.setVisibility(View.GONE); lineSignResult.setVisibility(View.VISIBLE); signAddress.setText(signIn.getAddress()); signInTime.setText(signIn.getTime()); signInResult.setText(getString(R.string.sign_in_success)); } } else { isAgain = false; } } }); } private void setLocation() { // 初始化 LocationClient mLocationService = new LocationService(this); // 注冊監聽 mLocationService.registerListener(mListener); LocationClientOption option = mLocationService.getOption(); // 簽到場景 只進行一次定位返回最接近真實位置的定位結果(定位速度可能會延遲1-3s) option.setLocationPurpose(LocationClientOption.BDLocationPurpose.SignIn); // 設置定位參數 mLocationService.setLocationOption(option); } /***** * * 定位結果回調,重寫onReceiveLocation方法 * */ private BDAbstractLocationListener mListener = new BDAbstractLocationListener() { /** * 定位請求回調函數 * * @param location 定位結果 */ @Override public void onReceiveLocation(BDLocation location) { if (null != location && location.getLocType() != BDLocation.TypeServerError && location.getLocType() != BDLocation.TypeOffLineLocationFail && location.getLocType() != BDLocation.TypeCriteriaException) { String address = location.getAddrStr(); //獲取詳細地址信息 if (!isAgain) { saveSignIn(address); } else { updateSignIn(address); } } else { signInResult.setText(getString(R.string.sign_in_failure)); } } }; private void setDate() { String dateString = FormatUtils.getDateTimeString(Calendar.getInstance().getTime(), FormatUtils.template_Date); String weekString = DateUtils.getDayOfWeek(); String CalendarString = dateString + ' ' + weekString; signCalender.setText(CalendarString); } @OnClick({R.id.btn_sign_in, R.id.btn_sign_again}) public void onClick(View view) { switch (view.getId()) { case R.id.btn_sign_in: signIn(); break; case R.id.btn_sign_again: isAgain = true; signIn(); break; default: } } /** * 更新簽到數據 * @param address */ private void updateSignIn(String address) { Calendar calendar = Calendar.getInstance(); SignIn newSignIn = new SignIn(); newSignIn.setUsername(myUser.getUsername()); newSignIn.setAddress(address); signIn.setDate(FormatUtils.getDateTimeString(calendar.getTime(), FormatUtils.template_Date)); signIn.setTime(FormatUtils.getDateTimeString(calendar.getTime(), FormatUtils.template_Time)); newSignIn.update(signIn.getObjectId(), new UpdateListener() { @Override public void done(BmobException e) { if (e == null) { signAddress.setText(address); signInTime.setText(FormatUtils.getDateTimeString(Calendar.getInstance().getTime(), FormatUtils.template_Time)); signInResult.setText(getString(R.string.sign_in_success)); ToastUtils.showShort(SignInActivity.this, getString(R.string.sign_in_success)); } else { ToastUtils.showShort(SignInActivity.this, getString(R.string.sign_in_failure)); } } }); } /** * 保存簽到數據 * @param address */ private void saveSignIn(String address) { Calendar calendar = Calendar.getInstance(); signIn.setUsername(myUser.getUsername()); signIn.setAddress(address); signIn.setDate(FormatUtils.getDateTimeString(calendar.getTime(), FormatUtils.template_Date)); signIn.setTime(FormatUtils.getDateTimeString(calendar.getTime(), FormatUtils.template_Time)); signIn.save(new SaveListener<String>() { @Override public void done(String s, BmobException e) { if (e == null) { btnSignIn.setVisibility(View.GONE); lineSignResult.setVisibility(View.VISIBLE); signAddress.setText(address); signInTime.setText(FormatUtils.getDateTimeString(Calendar.getInstance().getTime(), FormatUtils.template_Time)); signInResult.setText(getString(R.string.sign_in_success)); ToastUtils.showShort(SignInActivity.this, getString(R.string.sign_in_success)); } else { ToastUtils.showShort(SignInActivity.this, getString(R.string.sign_in_failure)); } } }); } /** * 簽到 */ private void signIn() { if (mLocationService.isStart()) { mLocationService.requestLocation(); return; } //簽到只需調用startLocation即可 mLocationService.start(); } @Override protected void onDestroy() { super.onDestroy(); if (mLocationService != null) { mLocationService.unregisterListener(mListener); mLocationService.stop(); } }}

歷史簽到布局

<?xml version='1.0' encoding='utf-8'?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:background='@color/color_ffffff' tools:context='.activity.MySignInActivity'> <LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:layout_margin='10dp' android:orientation='vertical'> <com.prolificinteractive.materialcalendarview.MaterialCalendarView android: android:layout_width='match_parent' android:layout_height='300dp' android:background='@color/white' android:clipChildren='false' app:mcv_calendarMode='month' app:mcv_dateTextAppearance='@style/MaterialCalendarTextStyelNormal' app:mcv_firstDayOfWeek='sunday' app:mcv_selectionColor='#D203A9F4' app:mcv_showOtherDates='all' app:mcv_tileSize='match_parent' app:mcv_tileWidth='match_parent' /> <View /> <LinearLayout android: android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_margin='15dp' android:visibility='gone' android:orientation='horizontal'> <ImageView android:layout_width='40dp' android:layout_height='40dp' android:layout_margin='5dp' android:src='http://www.hdgsjgj.cn/bcjs/@mipmap/sign_in_address' /> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='vertical'> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content'> <TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:textColor='@color/color_000000' android:textSize='18sp' android:text='@string/sign_in_time'/> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:textColor='@color/color_000000' android:textSize='18sp' android:layout_marginLeft='5dp'/> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:textColor='@color/color_000000' android:textSize='18sp' android:layout_marginLeft='5dp'/> </LinearLayout> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginLeft='5dp'/> </LinearLayout> </LinearLayout> </LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

MySignInActivity.java

public class MySignInActivity extends BaseActivity implements OnDateSelectedListener { @BindView(R.id.my_sign_in_date) TextView mySignInDate; @BindView(R.id.my_sign_in_time) TextView mySignInTime; @BindView(R.id.my_sign_in_address) TextView mySignInAddress; @BindView(R.id.line_my_sign_in) LinearLayout lineMySignIn; @BindView(R.id.materialCalendarView_sign_in) MaterialCalendarView widget; MyUser myUser = BmobUser.getCurrentUser(MyUser.class); private List<CalendarDay> calendarDays = new ArrayList<>(); @Override protected int contentViewID() { return R.layout.activity_my_sign_in; } @Override protected void initialize() { setTopTitle(getString(R.string.my_sign_in), true); setLeftBtnFinish(); widget.setSelectedDate(CalendarDay.today()); widget.state().edit().setMaximumDate(CalendarDay.today()).commit(); widget.setOnDateChangedListener(this); initDate(); querySignInState(Calendar.getInstance()); } private void initDate() { BmobQuery<SignIn> signInBmobQuery = new BmobQuery<SignIn>(); signInBmobQuery.addWhereEqualTo('username', myUser.getUsername()); signInBmobQuery.findObjects(new FindListener<SignIn>() { @Override public void done(List<SignIn> object, BmobException e) { if (e == null) { if (!object.isEmpty()) { for (SignIn signIn : object) { Date date = DateUtils.strToDate(signIn.getDate() + ' ' + signIn.getTime()); calendarDays.add(CalendarDay.from(date)); } widget.addDecorator(new EventDecorator(ContextCompat.getColor(MySignInActivity.this, R.color.color_1396aa), calendarDays)); } } else { LogUtils.e(e.getMessage()); ToastUtils.showShort(MySignInActivity.this, getString(R.string.query_failure)); } } }); } @Override public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) { querySignInState(date.getCalendar()); } private void querySignInState(Calendar calendar) { BmobQuery<SignIn> signInBmobQuery = new BmobQuery<SignIn>(); signInBmobQuery.addWhereEqualTo('username', myUser.getUsername()); signInBmobQuery.addWhereEqualTo('date', FormatUtils.getDateTimeString(calendar.getTime(), FormatUtils.template_Date)); signInBmobQuery.findObjects(new FindListener<SignIn>() { @Override public void done(List<SignIn> object, BmobException e) { if (e == null) { if (!object.isEmpty()) { lineMySignIn.setVisibility(View.VISIBLE); SignIn signIn = object.get(0); mySignInDate.setText(signIn.getDate()); mySignInTime.setText(signIn.getTime()); mySignInAddress.setText(signIn.getAddress()); } else { lineMySignIn.setVisibility(View.GONE); } } else { ToastUtils.showShort(MySignInActivity.this, getString(R.string.query_failure)); } } }); } }

日歷小圓點裝飾,重寫 DayViewDecorator

public class EventDecorator implements DayViewDecorator { private int color; private HashSet<CalendarDay> dates; public EventDecorator(int color, Collection<CalendarDay> dates) { this.color = color; this.dates = new HashSet<>(dates); } @Override public boolean shouldDecorate(CalendarDay day) { return dates.contains(day); } @Override public void decorate(DayViewFacade view) { view.addSpan(new DotSpan(7, color)); }}

總結

到此這篇關于Android項目實戰之地點簽到功能(百度地圖)的文章就介紹到這了,更多相關android 地點簽到內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: 百度 地圖
相關文章:
主站蜘蛛池模板: 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 合肥角钢_合肥槽钢_安徽镀锌管厂家-昆瑟商贸有限公司 | 回转窑-水泥|石灰|冶金-巩义市瑞光金属制品有限责任公司 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 东莞画册设计_logo/vi设计_品牌包装设计 - 华略品牌设计公司 | 菏泽知彼网络科技有限公司 | 工业插头-工业插头插座【厂家】-温州罗曼电气 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 屏蔽泵厂家,化工屏蔽泵_维修-淄博泵业 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 氧化铝球_高铝球_氧化铝研磨球-淄博誉洁陶瓷新材料有限公司 | 小型铜米机-干式铜米机-杂线全自动铜米机-河南鑫世昌机械制造有限公司 | 集装袋吨袋生产厂家-噸袋廠傢-塑料编织袋-纸塑复合袋-二手吨袋-太空袋-曹县建烨包装 | 数控专用机床,专用机床,自动线,组合机床,动力头,自动化加工生产线,江苏海鑫机床有限公司 | 硬度计,金相磨抛机_厂家-莱州华煜众信试验仪器有限公司 | 玻璃钢格栅盖板|玻璃钢盖板|玻璃钢格栅板|树篦子-长沙川皖玻璃钢制品有限公司 | 温州富欧金属封头-不锈钢封头厂家 | 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 | 小区健身器材_户外健身器材_室外健身器材_公园健身路径-沧州浩然体育器材有限公司 | 杰福伦_磁致伸缩位移传感器_线性位移传感器-意大利GEFRAN杰福伦-河南赉威液压科技有限公司 | 依维柯自动挡房车,自行式国产改装房车,小型房车价格,中国十大房车品牌_南京拓锐斯特房车 - 南京拓锐斯特房车 | 3D全息投影_地面互动投影_360度立体投影_水幕灯光秀 | 广东银虎 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 国际船舶网 - 船厂、船舶、造船、船舶设备、航运及海洋工程等相关行业综合信息平台 | 企业微信scrm管理系统_客户关系管理平台_私域流量运营工具_CRM、ERP、OA软件-腾辉网络 | 广州监控安装公司_远程监控_安防弱电工程_无线wifi覆盖_泉威安防科技 | 章丘丰源机械有限公司 - 三叶罗茨风机,罗茨鼓风机,罗茨风机 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 吉林污水处理公司,长春工业污水处理设备,净水设备-长春易洁环保科技有限公司 | 耐酸碱胶管_耐腐蚀软管总成_化学品输送软管_漯河利通液压科技耐油耐磨喷砂软管|耐腐蚀化学软管 | 上海防爆真空干燥箱-上海防爆冷库-上海防爆冷柜?-上海浦下防爆设备厂家? | 大立教育官网-一级建造师培训-二级建造师培训-造价工程师-安全工程师-监理工程师考试培训 | 气密性检测仪_气密性检测设备_防水测试仪_密封测试仪-岳信仪器 | 北京发电机出租_发电机租赁_北京发电机维修 - 河北腾伦发电机出租 | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | ◆大型吹塑加工|吹塑加工|吹塑代加工|吹塑加工厂|吹塑设备|滚塑加工|滚塑代加工-莱力奇塑业有限公司 | 广州展台特装搭建商|特装展位设计搭建|展会特装搭建|特装展台制作设计|展览特装公司 | 重庆网站建设,重庆网站设计,重庆网站制作,重庆seo,重庆做网站,重庆seo,重庆公众号运营,重庆小程序开发 | 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 |