Android高德地图获取当前地理位置(不显示地图只获取当前位置)


一、集成和权限配置
参考:http://blog.csdn.net/weixin_37577039/article/details/79177131

二、

//声明AMapLocationClient类对象    AMapLocationClient mLocationClient = null;    //声明AMapLocationClientOption对象    public AMapLocationClientOption mLocationOption = null;

onCreate()中

getCurrentLocationLatLng();

三、初始化定位并设置定位回调监听

/***  初始化定位并设置定位回调监听*/private void getCurrentLocationLatLng(){    //初始化定位    mLocationClient = new AMapLocationClient(getApplicationContext());   //设置定位回调监听    mLocationClient.setLocationListener(mLocationListener);    //初始化AMapLocationClientOption对象    mLocationOption = new AMapLocationClientOption(); /* //设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景) 设置了场景就不用配置定位模式等    option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);    if(null != locationClient){        locationClient.setLocationOption(option);        //设置场景模式后最好调用一次stop,再调用start以保证场景模式生效        locationClient.stopLocation();        locationClient.startLocation();    }*/    // 同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);    //只会使用网络定位   /* mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);*/   //只使用GPS进行定位    /*mLocationOption.setLocationMode(AMapLocationMode.Device_Sensors);*/    // 设置为单次定位 默认为false    /*mLocationOption.setOnceLocation(true);*/    //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。默认连续定位 切最低时间间隔为1000ms    mLocationOption.setInterval(3500);    //设置是否返回地址信息(默认返回地址信息)    /*mLocationOption.setNeedAddress(true);*/    //关闭缓存机制 默认开启 ,在高精度模式和低功耗模式下进行的网络定位结果均会生成本地缓存,不区分单次定位还是连续定位。GPS定位结果不会被缓存。    /*mLocationOption.setLocationCacheEnable(false);*/    //给定位客户端对象设置定位参数    mLocationClient.setLocationOption(mLocationOption);    //启动定位    mLocationClient.startLocation();}

四、定位回调监听器

/** * 定位回调监听器 */public AMapLocationListener mLocationListener = new AMapLocationListener() {    @Override    public void onLocationChanged(AMapLocation amapLocation) {        if (!IsGpsWork.isGpsEnabled(getApplicationContext())) {            Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.hasNotOpenGps), Toast.LENGTH_SHORT);            toast.setGravity(Gravity.CENTER, 0, 0);            toast.show();        } else {            if (amapLocation != null) {                if (amapLocation.getErrorCode() == 0) {                    //定位成功回调信息,设置相关消息                    amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表                    double currentLat = amapLocation.getLatitude();//获取纬度                    double currentLon = amapLocation.getLongitude();//获取经度                    latLonPoint = new LatLonPoint(currentLat, currentLon);  // latlng形式的                /*currentLatLng = new LatLng(currentLat, currentLon);*/   //latlng形式的                    Log.i("currentLocation", "currentLat : " + currentLat + " currentLon : " + currentLon);                    amapLocation.getAccuracy();//获取精度信息                } else {                    //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。                    Log.e("AmapError", "location Error, ErrCode:"                            + amapLocation.getErrorCode() + ", errInfo:"                            + amapLocation.getErrorInfo());                }            }        }    }};

 @Override    protected void onDestroy() {        super.onDestroy();        if(mLocationClient!=null) {            mLocationClient.onDestroy();//销毁定位客户端。        }    }    @Override    protected void onStart() {        super.onStart();        if(mLocationClient!=null) {            mLocationClient.startLocation(); // 启动定位        }    }    @Override    protected void onPause() {        super.onPause();        if(mLocationClient!=null) {            mLocationClient.stopLocation();//停止定位        }    }

获取当前位置具体描述(逆地址编码),参考:
http://blog.csdn.net/weixin_37577039/article/details/79177992


参考:高德地图官方文档:http://developer.amap.com/api/android-location-sdk/guide/android-location/getlocation

更多相关文章

  1. Android中添加布局和初始化布局总结
  2. android appwidget service的初始化
  3. Android初始化语言 (init.*.rc、init.conf文件格式)
  4. android注解初始化view
  5. Android 应用初始化及窗体事件的分发
  6. [android]初始化代码仓库时出现“OSError: [Errno 2] No such fi
  7. -----------Android Support v4、v7、v13的区别和应用场景
  8. Android 初始化之Zygote

随机推荐

  1. 一定要改掉 这5个PHP编程中的不良习惯!
  2. PHP安全问题汇总
  3. 详解PHP中被忽略的性能优化利器:生成器
  4. 教你使用PHP实现查找你想要的附近人
  5. 你可能要纠正这5个PHP编码小陋习!
  6. PHP处理时间和时区需注意以下三点!
  7. php+redis实现全页缓存系统
  8. 分享php秒杀功能实现的思路
  9. 分享五个与PHP有关的技术大会!【整理推荐
  10. linux与windows下安装ImageMagick及php i