重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android如何实现百度地图地点签到功能

小编这次要给大家分享的是Android如何实现百度地图地点签到功能,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

创新互联公司主要从事做网站、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务罗江,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

前言:先写个简单的地点签到功能,如果日后有时间细写的话,会更加好好研究一下百度地图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"?>

 
 
 
  
 
  
 
  
 
  
 
   
 
   
 
    
 
    
 
     
 
     
    
 
    
 
    

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 signInBmobQuery = new BmobQuery();
  signInBmobQuery.addWhereEqualTo("username", myUser.getUsername());
  signInBmobQuery.addWhereEqualTo("date", FormatUtils.getDateTimeString(Calendar.getInstance().getTime(), FormatUtils.template_Date));
  signInBmobQuery.findObjects(new FindListener() {
   @Override
   public void done(List 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() {
   @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"?>

 
 
  
 
  
  
   
 
   
    
     
     
     
    
 
    
   
  
 
 

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 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 signInBmobQuery = new BmobQuery();
  signInBmobQuery.addWhereEqualTo("username", myUser.getUsername());
  signInBmobQuery.findObjects(new FindListener() {
   @Override
   public void done(List 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 signInBmobQuery = new BmobQuery();
  signInBmobQuery.addWhereEqualTo("username", myUser.getUsername());
  signInBmobQuery.addWhereEqualTo("date", FormatUtils.getDateTimeString(calendar.getTime(), FormatUtils.template_Date));
  signInBmobQuery.findObjects(new FindListener() {
   @Override
   public void done(List 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 dates;
 
 public EventDecorator(int color, Collection 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如何实现百度地图地点签到功能
文章出自:http://cqcxhl.cn/article/jdjhjo.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP