Android项目实战之百度地图地点签到功能
程序员文章站
2023-12-30 21:56:34
前言:先写个简单的地点签到功能,如果日后有时间细写的话,会更加好好研究一下百度地图api,做更多逻辑判断。这里主要是调用百度地图中的场景定位中的签到场景。通过官方文档进行api集成。通过gps的定位功...
前言:先写个简单的地点签到功能,如果日后有时间细写的话,会更加好好研究一下百度地图api,做更多逻辑判断。
这里主要是调用百度地图中的场景定位中的签到场景。通过官方文档进行api集成。通过gps的定位功能,获取地理位置,时间,用户名进行存储。之后通过日历显示历史签到记录。
效果图:
/**百度地图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:id="@+id/sign_calendar" 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 style="@style/horizontal_line_style" /> <linearlayout android:id="@+id/line_sign_result" 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="@mipmap/sign_in_address" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:id="@+id/sign_in_result" 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:id="@+id/sign_in_time" android:layout_width="match_parent" android:layout_height="wrap_content" /> </linearlayout> <textview android:id="@+id/sign_address" android:layout_width="match_parent" android:layout_height="wrap_content" /> <button android:id="@+id/btn_sign_again" 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:id="@+id/btn_sign_in" style="@style/style_button" 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:id="@+id/materialcalendarview_sign_in" 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 style="@style/horizontal_line_style"/> <linearlayout android:id="@+id/line_my_sign_in" 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="@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:id="@+id/my_sign_in_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="@color/color_000000" android:textsize="18sp" android:layout_marginleft="5dp"/> <textview android:id="@+id/my_sign_in_time" 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:id="@+id/my_sign_in_address" 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 地点签到内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!