Android項目實戰(zhàn)之百度地圖地點簽到功能
前言:先寫個簡單的地點簽到功能,如果日后有時間細寫的話,會更加好好研究一下百度地圖api,做更多邏輯判斷。
這里主要是調(diào)用百度地圖中的場景定位中的簽到場景。通過官方文檔進行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();
}
/**
* 查詢今日簽到狀態(tài)
*/
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);
// 注冊監(jiān)聽
mLocationService.registerListener(mListener);
LocationClientOption option = mLocationService.getOption();
// 簽到場景 只進行一次定位返回最接近真實位置的定位結(jié)果(定位速度可能會延遲1-3s)
option.setLocationPurpose(LocationClientOption.BDLocationPurpose.SignIn);
// 設(shè)置定位參數(shù)
mLocationService.setLocationOption(option);
}
/*****
*
* 定位結(jié)果回調(diào),重寫onReceiveLocation方法
*
*/
private BDAbstractLocationListener mListener = new BDAbstractLocationListener() {
/**
* 定位請求回調(diào)函數(shù)
*
* @param location 定位結(jié)果
*/
@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:
}
}
/**
* 更新簽到數(shù)據(jù)
* @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));
}
}
});
}
/**
* 保存簽到數(shù)據(jù)
* @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;
}
//簽到只需調(diào)用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));
}
}
總結(jié)
到此這篇關(guān)于Android項目實戰(zhàn)之地點簽到功能(百度地圖)的文章就介紹到這了,更多相關(guān)android 地點簽到內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android webview旋轉(zhuǎn)屏幕導(dǎo)致頁面重新加載問題解決辦法
這篇文章主要介紹了Android webview旋轉(zhuǎn)屏幕導(dǎo)致頁面重新加載問題解決辦法的相關(guān)資料,希望通過本文能幫助到大家實現(xiàn)這樣的問題,需要的朋友可以參考下2017-10-10
Android應(yīng)用圖標在狀態(tài)欄上顯示實現(xiàn)原理
Android應(yīng)用圖標在狀態(tài)欄上顯示,以及顯示不同的圖標,其實很研究完后,才發(fā)現(xiàn),很簡單,具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android 利用ViewPager+GridView實現(xiàn)首頁導(dǎo)航欄布局分頁效果
用ViewPager+GridView實現(xiàn)首頁導(dǎo)航欄布局分頁效果來實現(xiàn)的效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-10-10
android surfaceView實現(xiàn)播放視頻功能
這篇文章主要為大家詳細介紹了android surfaceView實現(xiàn)播放視頻功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05

