Search in sources :

Example 16 with AMapLocationClient

use of com.amap.api.location.AMapLocationClient in project SeeWeather by xcc3641.

the class MainFragment method location.

/**
 * 高德定位
 */
private void location() {
    // 初始化定位
    mLocationClient = new AMapLocationClient(getActivity());
    mLocationOption = new AMapLocationClientOption();
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
    mLocationOption.setNeedAddress(true);
    mLocationOption.setOnceLocation(true);
    mLocationOption.setWifiActiveScan(false);
    // 设置定位间隔 单位毫秒
    int autoUpdateTime = SharedPreferenceUtil.getInstance().getAutoUpdate();
    mLocationOption.setInterval((autoUpdateTime == 0 ? 100 : autoUpdateTime) * SharedPreferenceUtil.ONE_HOUR);
    mLocationClient.setLocationOption(mLocationOption);
    mLocationClient.setLocationListener(aMapLocation -> {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                aMapLocation.getLocationType();
                SharedPreferenceUtil.getInstance().setCityName(Util.replaceCity(aMapLocation.getCity()));
            } else {
                if (isAdded()) {
                    ToastUtil.showShort(getString(R.string.errorLocation));
                }
            }
            load();
        }
    });
    mLocationClient.startLocation();
}
Also used : AMapLocationClient(com.amap.api.location.AMapLocationClient) AMapLocationClientOption(com.amap.api.location.AMapLocationClientOption)

Example 17 with AMapLocationClient

use of com.amap.api.location.AMapLocationClient in project ride-read-android by Ride-Read.

the class AMapLocationUtils method init.

public static void init() {
    // 初始化定位
    mLocationClient = new AMapLocationClient(Utils.getAppContext());
    // 设置定位回调监听
    mLocationClient.setLocationListener(mAMapLocationListener);
    // 声明AMapLocationClientOption对象
    AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
    // 设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    mLocationOption.setOnceLocation(true);
    mLocationOption.setOnceLocationLatest(false);
    mLocationOption.setNeedAddress(true);
    mLocationOption.setWifiActiveScan(false);
    // //设置定位间隔,单位毫秒,默认为5000ms
    mLocationOption.setInterval(60 * 1000);
    mLocationOption.setLocationCacheEnable(true);
    // 给定位客户端对象设置定位参数
    mLocationClient.setLocationOption(mLocationOption);
    // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
    // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
    // 在定位结束后,在合适的生命周期调用onDestroy()方法
    // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
    // 启动定位
    mLocationClient.startLocation();
}
Also used : AMapLocationClient(com.amap.api.location.AMapLocationClient) AMapLocationClientOption(com.amap.api.location.AMapLocationClientOption)

Aggregations

AMapLocationClient (com.amap.api.location.AMapLocationClient)17 AMapLocationClientOption (com.amap.api.location.AMapLocationClientOption)10 AMapLocation (com.amap.api.location.AMapLocation)4 AMapLocationListener (com.amap.api.location.AMapLocationListener)4 Message (android.os.Message)1