Search in sources :

Example 16 with AMapLocationClientOption

use of com.amap.api.location.AMapLocationClientOption 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 AMapLocationClientOption

use of com.amap.api.location.AMapLocationClientOption in project JustAndroid by chinaltz.

the class LocationProvider method getDefaultOption.

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

Example 18 with AMapLocationClientOption

use of com.amap.api.location.AMapLocationClientOption 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

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