123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package com.ynstkz.shitu.android.activity;
- import android.Manifest;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.app.Fragment;
- import android.support.v4.content.ContextCompat;
- import android.util.Log;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import com.amap.api.location.AMapLocation;
- import com.amap.api.location.AMapLocationClient;
- import com.amap.api.location.AMapLocationClientOption;
- import com.amap.api.location.AMapLocationListener;
- import com.ynstkz.shitu.android.R;
- import com.ynstkz.shitu.android.base.TitleBarActivity;
- import com.ynstkz.shitu.android.fragment.ConfirmationDialogFragment;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import butterknife.Bind;
- import butterknife.ButterKnife;
- public class HomeActivity extends TitleBarActivity implements AMapLocationListener {
- private static final String FRAGMENT_DIALOG = "dialog";
- private static final int REQUEST_CAMERA_PERMISSION = 1;
- @Bind(R.id.rl_lab_home)
- RelativeLayout rlLabHome;
- @Bind(R.id.rl_lab_nearby)
- RelativeLayout rlLabNearby;
- @Bind(R.id.rl_lab_usercenter)
- RelativeLayout rlLabUsercenter;
- @Bind(R.id.ll_bottom_lab)
- LinearLayout llBottomLab;
- private Fragment[] mFragments;
- private int tabIndex;
- //定位
- public AMapLocationClient mlocationClient;
- //声明mLocationOption对象
- public AMapLocationClientOption mLocationOption = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ButterKnife.bind(this);
- initView();
- initData();
- setListener();
- }
- @Override
- protected int getLayoutId() {
- return R.layout.activity_home;
- }
- private void initView(){
- mFragments = new Fragment[3];
- mFragments[0] = getSupportFragmentManager().findFragmentById(R.id.fm_home);
- mFragments[1] = getSupportFragmentManager().findFragmentById(R.id.fm_nearby);
- mFragments[2] = getSupportFragmentManager().findFragmentById(R.id.fm_usercent);
- changleLayout(0);
- }
- private void initData(){
- mlocationClient = new AMapLocationClient(getApplicationContext());
- //初始化定位参数
- mLocationOption = new AMapLocationClientOption();
- //设置定位监听
- mlocationClient.setLocationListener(this);
- //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
- mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
- //获取一次定位结果:
- mLocationOption.setOnceLocation(true);
- //设置是否返回地址信息(默认返回地址信息)
- mLocationOption.setNeedAddress(true);
- //设置定位参数
- mlocationClient.setLocationOption(mLocationOption);
- }
- private void setListener(){
- //首页
- rlLabHome.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- changleLayout(0);
- }
- });
- //附近
- rlLabNearby.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- changleLayout(1);
- }
- });
- //个人中心
- rlLabUsercenter.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if(isLogin()){
- changleLayout(2);
- } else {
- startActivityForResult(new Intent(HomeActivity.this, LoginActivity.class), 10);
- }
- }
- });
- }
- /**
- * 切换fragment
- *
- * @param tabIndex
- */
- private void changleLayout(int tabIndex) {
- this.tabIndex = tabIndex;
- getSupportFragmentManager().beginTransaction().hide(mFragments[0]).hide(mFragments[1])
- .hide(mFragments[2]).show(mFragments[tabIndex]).commitAllowingStateLoss();
- switch (tabIndex) {
- }
- }
- @Override
- public void onLocationChanged(AMapLocation amapLocation) {
- if (amapLocation != null) {
- if (amapLocation.getErrorCode() == 0) {
- //定位成功回调信息,设置相关消息
- amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
- amapLocation.getLatitude();//获取纬度
- amapLocation.getLongitude();//获取经度
- amapLocation.getAccuracy();//获取精度信息
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = new Date(amapLocation.getTime());
- df.format(date);//定位时间
- } else {
- //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
- Log.e("AmapError", "location Error, ErrCode:"
- + amapLocation.getErrorCode() + ", errInfo:"
- + amapLocation.getErrorInfo());
- }
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- //启动定位
- mlocationClient.startLocation();
- } else if (ActivityCompat.shouldShowRequestPermissionRationale(this,
- Manifest.permission.ACCESS_COARSE_LOCATION)){
- ConfirmationDialogFragment
- .newInstance(R.string.app_name,
- new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
- REQUEST_CAMERA_PERMISSION,
- R.string.app_name)
- .show(getSupportFragmentManager(), FRAGMENT_DIALOG);
- } else {
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
- REQUEST_CAMERA_PERMISSION);
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- switch (requestCode) {
- case 10:
- if(resultCode == RESULT_OK) { //登陆成功
- changleLayout(2);
- }
- break;
- }
- }
- }
|