content: package com.zhongou.view;
  
  import android.graphics.BitmapFactory;
  import android.os.Bundle;
  import android.os.Message;
  import android.text.TextUtils;
  import android.util.Log;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  import android.widget.Toast;
  
  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.amap.api.maps2d.AMap;
  import com.amap.api.maps2d.CameraUpdateFactory;
  import com.amap.api.maps2d.LocationSource;
  import com.amap.api.maps2d.MapView;
  import com.amap.api.maps2d.UiSettings;
  import com.amap.api.maps2d.model.BitmapDescriptor;
  import com.amap.api.maps2d.model.BitmapDescriptorFactory;
  import com.amap.api.maps2d.model.LatLng;
  import com.amap.api.maps2d.model.MarkerOptions;
  import com.zhongou.R;
  import com.zhongou.base.BaseActivity;
  import com.zhongou.common.MyException;
  import com.zhongou.dialog.Loading;
  import com.zhongou.helper.UserHelper;
  import com.zhongou.inject.ViewInject;
  import com.zhongou.utils.PageUtil;
  
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  /**
   * 地图定位
   */
  public class MapLocationActivity extends BaseActivity implements LocationSource {
      @ViewInject(id=R.id.layout_back,click = "forBack")
      RelativeLayout forBack;
  
      @ViewInject(id = R.id.tv_right,click = "forAttend")
      TextView tv_right;
  
      @ViewInject(id = R.id.tv_title)
      TextView tvTitle;
      //AMap是地图对象
      private AMap aMap;
      private MapView mapView;
      //声明AMapLocationClient类对象，定位发起端
      private AMapLocationClient mLocationClient = null;
      //声明mLocationOption对象，定位参数
      public AMapLocationClientOption mLocationOption = null;
      //声明mListener对象，定位监听器
      private OnLocationChangedListener mListener = null;
      //标识，用于判断是否只显示一次定位信息和用户重新定位
      private boolean isFirstLoc = true;
      private ImageView imgBack = null;
      private StringBuffer buffer = null;
      private double lat ;
      private double lon ;
      private String currentTime = null;
      private String address = null;
  
      //常量
      private final int POST_SUCESS = 1001; // 登陆成功
      private final int POST_FAILED = 1002; // 失败
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.act_apps_map_location);
          tvTitle.setText(getResources().getString(R.string.mapAttendance));
          tv_right.setText(getResources().getString(R.string.forAttend));
  
          mapView = (MapView) findViewById(R.id.map_view);
          imgBack = (ImageView) findViewById(R.id.imgBack);
          imgBack.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  MapLocationActivity.this.finish();
              }
          });
          //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，实现地图生命周期管理
          mapView.onCreate(savedInstanceState);
          if (aMap == null) {
              aMap = mapView.getMap();
              //设置显示定位按钮 并且可以点击
              UiSettings settings = aMap.getUiSettings();
              aMap.setLocationSource(this);//设置了定位的监听
              // 是否显示定位按钮
              settings.setMyLocationButtonEnabled(true);
              aMap.setMyLocationEnabled(true);//显示定位层并且可以触发定位,默认是flase
          }
          //开始定位
          location();
  
          //添加marker
          aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 19));
          MarkerOptions markerOptions = new MarkerOptions();
          markerOptions.position(new LatLng(lat, lon));
          markerOptions.title("当前位置");
          markerOptions.visible(true);
          BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.location_marker));
          markerOptions.icon(bitmapDescriptor);
          aMap.addMarker(markerOptions);
      }
  
  
      public void forAttend(View view){
  
          if(TextUtils.isEmpty(currentTime)){
              PageUtil.DisplayToast("没有获取时间");
              return;
          }
          if (TextUtils.isEmpty(address)) {
              PageUtil.DisplayToast("没有获取到地址");
              return;
          }
  
  
          Loading.run(this, new Runnable() {
  
              @Override
              public void run() {
                try {
                      UserHelper.forAttend(MapLocationActivity.this, currentTime, address);
                      // 访问服务端成功，消息处理
                      sendMessage(POST_SUCESS);
                  } catch (MyException e) {
                      sendMessage(POST_FAILED, e.getMessage());
                 }
              }
          });
  
      }
  
      @Override
      protected void handleMessage(Message msg) {
          super.handleMessage(msg);
          switch (msg.what){
              case POST_FAILED:
                  PageUtil.DisplayToast((String)msg.obj);
                  break;
              case POST_SUCESS:
                  PageUtil.DisplayToast("签到成功！");
                  break;
              default:
                  break;
          }
      }
  
      public AMapLocationListener mLocationListener = new AMapLocationListener() {
          @Override
          public void onLocationChanged(AMapLocation aMapLocation) {
              if (aMapLocation != null) {
                  if (aMapLocation.getErrorCode() == 0) {
                      //定位成功回调信息，设置相关消息
                      aMapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见官方定位类型表
                      lat =  aMapLocation.getLatitude();//获取纬度
                      lon = aMapLocation.getLongitude();//获取经度
                      aMapLocation.getAccuracy();//获取精度信息
                      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                      Date date = new Date(aMapLocation.getTime());
                      currentTime = df.format(date);//定位时间
                      address = aMapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                      aMapLocation.getCountry();//国家信息
                      aMapLocation.getProvince();//省信息
                      aMapLocation.getCity();//城市信息
                      aMapLocation.getDistrict();//城区信息
                      aMapLocation.getStreet();//街道信息
                      aMapLocation.getStreetNum();//街道门牌号信息
                      aMapLocation.getCityCode();//城市编码
                      aMapLocation.getAdCode();//地区编码
  
                      // 如果不设置标志位，此时再拖动地图时，它会不断将地图移动到当前的位置
                      if (isFirstLoc) {
                          //设置缩放级别
                          aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
                          //将地图移动到定位点
                          aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
                          //点击定位按钮 能够将地图的中心移动到定位点
                          mListener.onLocationChanged(aMapLocation);
                          //添加图钉
                          //  aMap.addMarker(getMarkerOptions(amapLocation));
                          //获取定位信息
                          buffer = new StringBuffer();
                          buffer.append(aMapLocation.getCountry() + ""
                                  + aMapLocation.getProvince() + ""
                                  + aMapLocation.getCity() + ""
                                  + aMapLocation.getProvince() + ""
                                  + aMapLocation.getDistrict() + ""
                                  + aMapLocation.getStreet() + ""
                                  + aMapLocation.getStreetNum());
                          Toast.makeText(getApplicationContext(), buffer.toString(), Toast.LENGTH_LONG).show();
                          isFirstLoc = false;
                      }
  
  
                  } else {
                      //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                      Log.e("AmapError", "location Error, ErrCode:"
                              + aMapLocation.getErrorCode() + ", errInfo:"
                              + aMapLocation.getErrorInfo());
                      Toast.makeText(getApplicationContext(), "定位失败", Toast.LENGTH_LONG).show();
                  }
              }
          }
      };
  
      private void location() {
          //初始化定位
          mLocationClient = new AMapLocationClient(getApplicationContext());
          //设置定位回调监听
          mLocationClient.setLocationListener(mLocationListener);
          //初始化定位参数
          mLocationOption = new AMapLocationClientOption();
          //设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
          mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
          //设置是否返回地址信息（默认返回地址信息）
          mLocationOption.setNeedAddress(true);
          //设置是否只定位一次,默认为false
          mLocationOption.setOnceLocation(false);
          //设置是否强制刷新WIFI，默认为强制刷新
          mLocationOption.setWifiActiveScan(true);
          //设置是否允许模拟位置,默认为false，不允许模拟位置
          mLocationOption.setMockEnable(false);
          //设置定位间隔,单位毫秒,默认为2000ms
          mLocationOption.setInterval(3000);
          //给定位客户端对象设置定位参数
          mLocationClient.setLocationOption(mLocationOption);
          //启动定位
          mLocationClient.startLocation();
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          mapView.onResume();
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          mapView.onPause();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          mapView.onDestroy();
          mLocationClient.stopLocation();//停止定位
          mLocationClient.onDestroy();//销毁定位客户端。
      }
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          mapView.onSaveInstanceState(outState);
      }
  
      @Override
      public void activate(OnLocationChangedListener onLocationChangedListener) {
          mListener = onLocationChangedListener;
      }
  
      @Override
      public void deactivate() {
          mListener = null;
  
      }
      public void forBack(View view){
        this.finish();
      }
  }
  
  