高德地图

1:在官网下载相关的资源
2:创建工程,key和包名还有sha1一定要一致,这一步非常重要,
3:替换参数
4:运行 ok
public class Location_Activity extends CheckPermissionsActivity
implements
OnCheckedChangeListener,
OnClickListener{
private RadioGroup rgLocationMode;
private EditText etInterval;
private EditText etHttpTimeout;
private CheckBox cbOnceLocation;
private CheckBox cbAddress;
private CheckBox cbGpsFirst;
private CheckBox cbCacheAble;
private CheckBox cbOnceLastest;
private CheckBox cbSensorAble;
private TextView tvResult;
private Button btLocation;


private AMapLocationClient locationClient = null;
private AMapLocationClientOption locationOption = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
setTitle(R.string.title_location);

initView();

//初始化定位
initLocation();
}

//初始化控件
private void initView(){
rgLocationMode = (RadioGroup) findViewById(R.id.rg_locationMode);

etInterval = (EditText) findViewById(R.id.et_interval);
etHttpTimeout = (EditText) findViewById(R.id.et_httpTimeout);

cbOnceLocation = (CheckBox)findViewById(R.id.cb_onceLocation);
cbGpsFirst = (CheckBox) findViewById(R.id.cb_gpsFirst);
cbAddress = (CheckBox) findViewById(R.id.cb_needAddress);
cbCacheAble = (CheckBox) findViewById(R.id.cb_cacheAble);
cbOnceLastest = (CheckBox) findViewById(R.id.cb_onceLastest);
cbSensorAble = (CheckBox)findViewById(R.id.cb_sensorAble);


tvResult = (TextView) findViewById(R.id.tv_result);
btLocation = (Button) findViewById(R.id.bt_location);

rgLocationMode.setOnCheckedChangeListener(this);
btLocation.setOnClickListener(this);
}

@Override
protected void onDestroy() {
super.onDestroy();
destroyLocation();
}


@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (null == locationOption) {
locationOption = new AMapLocationClientOption();
}
switch (checkedId) {
case R.id.rb_batterySaving :
locationOption.setLocationMode(AMapLocationMode.Battery_Saving);
break;
case R.id.rb_deviceSensors :
locationOption.setLocationMode(AMapLocationMode.Device_Sensors);
break;
case R.id.rb_hightAccuracy :
locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
break;
default :
break;
}


}


/**
* 设置控件的可用状态
*/
private void setViewEnable(boolean isEnable) {
for(int i=0; i<rgLocationMode.getChildCount(); i++){
rgLocationMode.getChildAt(i).setEnabled(isEnable);
}
etInterval.setEnabled(isEnable);
etHttpTimeout.setEnabled(isEnable);
cbOnceLocation.setEnabled(isEnable);
cbGpsFirst.setEnabled(isEnable);
cbAddress.setEnabled(isEnable);
cbCacheAble.setEnabled(isEnable);
cbOnceLastest.setEnabled(isEnable);
cbSensorAble.setEnabled(isEnable);
}


@Override
public void onClick(View v) {
if (v.getId() == R.id.bt_location) {
if (btLocation.getText().equals(
getResources().getString(R.string.startLocation))) {
setViewEnable(false);
btLocation.setText(getResources().getString(
R.string.stopLocation));
tvResult.setText("正在定位...");
startLocation();
} else {
setViewEnable(true);
btLocation.setText(getResources().getString(
R.string.startLocation));
stopLocation();
tvResult.setText("定位停止");
}
}
}

/**
* 初始化定位

* @since 2.8.0
* @author hongming.wang
*
*/
private void initLocation(){
//初始化client
locationClient = new AMapLocationClient(this.getApplicationContext());
locationOption = getDefaultOption();
//设置定位参数
locationClient.setLocationOption(locationOption);
// 设置定位监听
locationClient.setLocationListener(locationListener);
}

/**
* 默认的定位参数
* @since 2.8.0
* @author hongming.wang
*
*/
private AMapLocationClientOption getDefaultOption(){
AMapLocationClientOption mOption = new AMapLocationClientOption();
mOption.setLocationMode(AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
mOption.setInterval(2000);//可选,设置定位间隔。默认为2秒
mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false
mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
return mOption;
}

/**
* 定位监听
*/
AMapLocationListener locationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation location) {
if (null != location) {


StringBuffer sb = new StringBuffer();
//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
if(location.getErrorCode() == 0){
sb.append("定位成功" + "\n");
sb.append("定位类型: " + location.getLocationType() + "\n");
sb.append("经    度    : " + location.getLongitude() + "\n");
sb.append("纬    度    : " + location.getLatitude() + "\n");
sb.append("精    度    : " + location.getAccuracy() + "米" + "\n");
sb.append("提供者    : " + location.getProvider() + "\n");


sb.append("速    度    : " + location.getSpeed() + "米/秒" + "\n");
sb.append("角    度    : " + location.getBearing() + "\n");
// 获取当前提供定位服务的卫星个数
sb.append("星    数    : " + location.getSatellites() + "\n");
sb.append("国    家    : " + location.getCountry() + "\n");
sb.append("省            : " + location.getProvince() + "\n");
sb.append("市            : " + location.getCity() + "\n");
sb.append("城市编码 : " + location.getCityCode() + "\n");
sb.append("区            : " + location.getDistrict() + "\n");
sb.append("区域 码   : " + location.getAdCode() + "\n");
sb.append("地    址    : " + location.getAddress() + "\n");
sb.append("兴趣点    : " + location.getPoiName() + "\n");
//定位完成的时间
sb.append("定位时间: " + Utils.formatUTC(location.getTime(), "yyyy-MM-dd HH:mm:ss") + "\n");
} else {
//定位失败
sb.append("定位失败" + "\n");
sb.append("错误码:" + location.getErrorCode() + "\n");
sb.append("错误信息:" + location.getErrorInfo() + "\n");
sb.append("错误描述:" + location.getLocationDetail() + "\n");
}
//定位之后的回调时间
sb.append("回调时间: " + Utils.formatUTC(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "\n");


//解析定位结果,
String result = sb.toString();
tvResult.setText(result);
} else {
tvResult.setText("定位失败,loc is null");
}
}
};

// 根据控件的选择,重新设置定位参数
private void resetOption() {
// 设置是否需要显示地址信息
locationOption.setNeedAddress(cbAddress.isChecked());
/**
* 设置是否优先返回GPS定位结果,如果30秒内GPS没有返回定位结果则进行网络定位
* 注意:只有在高精度模式下的单次定位有效,其他方式无效
*/
locationOption.setGpsFirst(cbGpsFirst.isChecked());
// 设置是否开启缓存
locationOption.setLocationCacheEnable(cbCacheAble.isChecked());
// 设置是否单次定位
locationOption.setOnceLocation(cbOnceLocation.isChecked());
//设置是否等待设备wifi刷新,如果设置为true,会自动变为单次定位,持续定位时不要使用
locationOption.setOnceLocationLatest(cbOnceLastest.isChecked());
//设置是否使用传感器
locationOption.setSensorEnable(cbSensorAble.isChecked());
//设置是否开启wifi扫描,如果设置为false时同时会停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
String strInterval = etInterval.getText().toString();
if (!TextUtils.isEmpty(strInterval)) {
try{
// 设置发送定位请求的时间间隔,最小值为1000,如果小于1000,按照1000算
locationOption.setInterval(Long.valueOf(strInterval));
}catch(Throwable e){
e.printStackTrace();
}
}

String strTimeout = etHttpTimeout.getText().toString();
if(!TextUtils.isEmpty(strTimeout)){
try{
// 设置网络请求超时时间
    locationOption.setHttpTimeOut(Long.valueOf(strTimeout));
}catch(Throwable e){
e.printStackTrace();
}
}
}


/**
* 开始定位

* @since 2.8.0
* @author hongming.wang
*
*/
private void startLocation(){
//根据控件的选择,重新设置定位参数
resetOption();
// 设置定位参数
locationClient.setLocationOption(locationOption);
// 启动定位
locationClient.startLocation();
}

/**
* 停止定位

* @since 2.8.0
* @author hongming.wang
*
*/
private void stopLocation(){
// 停止定位
locationClient.stopLocation();
}

/**
* 销毁定位

* @since 2.8.0
* @author hongming.wang
*
*/
private void destroyLocation(){
if (null != locationClient) {
/**
* 如果AMapLocationClient是在当前Activity实例化的,
* 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
*/
locationClient.onDestroy();
locationClient = null;
locationOption = null;
}
}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值