/** * 请求获取位置信息 * * @param onceLocation 是否只定位一次 * @param aMapLocationListener 定位回调接口,该回调接口中的方法不会立即被调用 */ public static void requestLocation( boolean onceLocation, AMapLocationListener aMapLocationListener) { stopLocation(); if (sAMapLocationClient == null) { sAMapLocationClientOption = new AMapLocationClientOption(); sAMapLocationClientOption.setLocationMode( AMapLocationClientOption.AMapLocationMode.Battery_Saving); sAMapLocationClientOption.setInterval(4000); sAMapLocationClient = new AMapLocationClient(App.getInstance()); } sAMapLocationClientOption.setOnceLocation(onceLocation); sAMapLocationClient.setLocationOption(sAMapLocationClientOption); sAMapLocationClient.setLocationListener(aMapLocationListener); sAMapLocationClient.startLocation(); }
/** 销毁定位客户端. 在Activity/Fragment的onDestroy方法中调用该方法 */ public static void onDestroy() { stopLocation(); if (sAMapLocationClient != null) { sAMapLocationClient.onDestroy(); } sAMapLocationClientOption = null; sAMapLocationClient = null; }
@Override public void handleMessage(Message msg) { if (msg.what == 0) { // 初始化数据 Bundle bundle = new Bundle(); boolean state = false; String data = msg.getData().getString("data"); GetUploadFileUrlReback result = new Gson().fromJson(data, GetUploadFileUrlReback.class); String message = result.getMes(); Log.e("myLog", "-----message" + message); if (result.getCode() == 0) { state = true; } bundle.putBoolean("state", state); bundle.putInt("type", 0); bundle.putString("msg", message); intent.putExtras(bundle); intent.setClass(AppSignActivity.this, AppSignResultActivity.class); startActivity(intent); finish(); } if (msg.what == 1) { Bundle bundle = new Bundle(); boolean state = false; String data = msg.getData().getString("data"); GetUploadFileUrlReback result = new Gson().fromJson(data, GetUploadFileUrlReback.class); String message = result.getMes(); Log.e("myLog", "-----message" + message); if (result.getCode() == 0) { state = true; } bundle.putBoolean("state", state); bundle.putInt("type", 1); bundle.putString("msg", message); intent.putExtras(bundle); intent.setClass(AppSignActivity.this, AppSignResultActivity.class); startActivity(intent); finish(); } if (msg.what == 2) { if (dialog == null) { return; } dialog.dismiss(); AMapLocation loc = (AMapLocation) msg.obj; Constant.location = loc; if (Constant.location.getErrorCode() == 0) { // 定位成功 wz.setText(Constant.location.getAddress()); } else { wz.setText("定位失败,请点击重试..."); Toast.makeText( AppSignActivity.this, "定位失败:" + Constant.location.getErrorInfo(), Toast.LENGTH_SHORT) .show(); } locationClient.stopLocation(); } }
public void getLocation() { if (MyUtils.selfPermissionGranted( Manifest.permission.ACCESS_COARSE_LOCATION, getApplicationContext())) { dialog.setCanceledOnTouchOutside(false); dialog.setMessage("正在定位,请稍后..."); dialog.show(); locationClient = new AMapLocationClient(getApplicationContext()); locationClient.setLocationListener(this); locationOption = new AMapLocationClientOption(); // 设置定位模式为低功耗模式 locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving); locationOption.setNeedAddress(true); // 设置定位参数 locationClient.setLocationOption(locationOption); // 启动定位 locationClient.startLocation(); } else { ActivityCompat.requestPermissions( instance, new String[] {Manifest.permission.ACCESS_COARSE_LOCATION}, Constant.LOCATION_STATE); } }
/** 停止定位. 在Activity/Fragment的onPause方法中调用该方法 */ public static void stopLocation() { if (sAMapLocationClient != null) { sAMapLocationClient.stopLocation(); } }