Java Code Examples for com.amap.api.location.AMapLocationClientOption#setOnceLocation()

The following examples show how to use com.amap.api.location.AMapLocationClientOption#setOnceLocation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewLocationManager.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
private AMapLocationClientOption getDefaultOption() {
        CommonLogger.e("获取定位选项");
        AMapLocationClientOption option = new AMapLocationClientOption();
//          定位模式:1 高精度、2仅设备、3仅网络
// 设置高精度模式
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
//                设置是否优先使用GPS
        option.setGpsFirst(false);
//                连接超时3秒
        option.setHttpTimeOut(3000);
//                设置定位间隔60秒
        option.setInterval(60000);
//                设置是否返回地址,默认返回
        option.setNeedAddress(true);
//                设置是否单次定位
        option.setOnceLocation(false);
        //可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
        option.setOnceLocationLatest(false);
//                设置网络请求协议
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);
//                设置是否使用传感器,不使用
        option.setSensorEnable(false);
        return option;
    }
 
Example 2
Source File: ClientOptionFactory.java    From RxLocation with Apache License 2.0 6 votes vote down vote up
/**
 * @return Default AMapLocationClientOption
 * 详情请查看{ http://amappc.cn-hangzhou.oss-pub.aliyun-inc.com/lbs/static/unzip/Android_Location_Doc/index.html }
 */
private AMapLocationClientOption getAMapDefaultOption() {
    final AMapLocationClientOption mOption = new AMapLocationClientOption();
    mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    mOption.setGpsFirst(false);
    mOption.setHttpTimeOut(30000);
    mOption.setInterval(2000);
    mOption.setNeedAddress(true);
    mOption.setOnceLocation(false);
    mOption.setOnceLocationLatest(false);
    AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);
    mOption.setSensorEnable(false);
    mOption.setWifiScan(true);
    mOption.setLocationCacheEnable(true);
    return mOption;
}
 
Example 3
Source File: LocationManager.java    From TestChat with Apache License 2.0 6 votes vote down vote up
private AMapLocationClientOption getDefaultOption() {
        LogUtil.e("获取定位选项");
        AMapLocationClientOption option = new AMapLocationClientOption();
//          定位模式:1 高精度、2仅设备、3仅网络
// 设置高精度模式
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
//                设置是否优先使用GPS
        option.setGpsFirst(false);
//                连接超时3秒
        option.setHttpTimeOut(3000);
//                设置定位间隔60秒
        option.setInterval(60000);
//                设置是否返回地址,默认返回
        option.setNeedAddress(true);
//                设置是否单次定位
        option.setOnceLocation(false);
        //可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
        option.setOnceLocationLatest(false);
//                设置网络请求协议
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);
//                设置是否使用传感器,不使用
        option.setSensorEnable(false);
        return option;
    }
 
Example 4
Source File: LocationModel.java    From Fishing with GNU General Public License v3.0 6 votes vote down vote up
public void startLocation(final Context ctx){
    AMapLocationClient mLocationClient = new AMapLocationClient(ctx);
    AMapLocationClientOption option = new AMapLocationClientOption();
    option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);

    //只取一次位置
    option.setOnceLocation(true);
    mLocationClient.setLocationOption(option);
    mLocationClient.setLocationListener(aMapLocation -> {
        JUtils.Log("GetLocation");
        //只有位置变动时才上传
        if (!mLocation.equals(createLocation(aMapLocation)))
            mLocationSubject.onNext(createLocation(aMapLocation));
    });
    mLocationClient.startLocation();
}
 
Example 5
Source File: MainActivity.java    From YourWeather with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化高德地图定位参数
 */
private void initLocation() {
    mLocationClient=new AMapLocationClient(getApplicationContext());
    mLocationClient.setLocationListener(mLocationListener);
    //初始化定位参数
    mLocationOption = new AMapLocationClientOption();
    //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    //设置是否返回地址信息(默认返回地址信息)
    mLocationOption.setNeedAddress(true);
    //设置是否只定位一次,默认为false
    mLocationOption.setOnceLocation(false);
    //设置是否强制刷新WIFI,默认为强制刷新
    mLocationOption.setWifiScan(true);
    //设置是否允许模拟位置,默认为false,不允许模拟位置
    mLocationOption.setMockEnable(false);
    //设置定位间隔,单位毫秒,默认为2000ms
    mLocationOption.setInterval(2000);
    //给定位客户端对象设置定位参数
    mLocationClient.setLocationOption(mLocationOption);
    // 启动定位
    mLocationClient.startLocation();
}
 
Example 6
Source File: MainActivity.java    From Ency with Apache License 2.0 6 votes vote down vote up
/**
 * 定位初始化
 */
private void initLocation() {
    //初始化定位
    mLocationClient = new AMapLocationClient(mContext);
    //设置定位回调监听
    mLocationClient.setLocationListener(this);
    mLocationOption = new AMapLocationClientOption();
    //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    //设置是否返回地址信息(默认返回地址信息)
    mLocationOption.setNeedAddress(true);
    //设置是否只定位一次,默认为false
    mLocationOption.setOnceLocation(false);
    //设置是否强制刷新WIFI,默认为强制刷新
    mLocationOption.setWifiActiveScan(true);
    //设置是否允许模拟位置,默认为false,不允许模拟位置
    mLocationOption.setMockEnable(false);
    //设置定位间隔 单位毫秒
    mLocationOption.setInterval(100 * 1000 * 60 * 60);
    //给定位客户端对象设置定位参数
    mLocationClient.setLocationOption(mLocationOption);
    //关闭缓存机制
    mLocationOption.setLocationCacheEnable(false);
    //启动定位
    mLocationClient.startLocation();
}
 
Example 7
Source File: WeatherActivity.java    From Pigeon with MIT License 6 votes vote down vote up
private void initLocation() {
    //初始化定位
    mLocationClient = new AMapLocationClient(getApplicationContext());
    //设置定位回调监听
    mLocationClient.setLocationListener(mLocationListener);

    //初始化AMapLocationClientOption对象
    mLocationOption = new AMapLocationClientOption();

    //高精度定位
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);

    //获取一次定位结果
    mLocationOption.setOnceLocation(true);

    //获取最近3s内精度最高的一次定位结果:
    mLocationOption.setOnceLocationLatest(true);

    mLocationClient.setLocationOption(mLocationOption);

    mLocationClient.startLocation();
}
 
Example 8
Source File: AMapLocationService.java    From GeometricWeather with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void requestLocation(Context context, @NonNull LocationCallback callback){
    this.callback = callback;

    AMapLocationClientOption option = new AMapLocationClientOption();
    option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
    option.setOnceLocation(true);
    option.setOnceLocationLatest(true);
    option.setNeedAddress(true);
    option.setMockEnable(false);
    option.setLocationCacheEnable(false);
    client = new AMapLocationClient(context.getApplicationContext());
    client.setLocationOption(option);
    client.setLocationListener(listener);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        manager.createNotificationChannel(getLocationNotificationChannel(context));
        client.enableBackgroundLocation(
                GeometricWeather.NOTIFICATION_ID_LOCATION,
                getLocationNotification(context));
    }
    client.startLocation();
}
 
Example 9
Source File: Location_Activity.java    From Android_Location_Demo with Apache License 2.0 6 votes vote down vote up
/**
 * 默认的定位参数
 * @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;
}
 
Example 10
Source File: FindMapAroundAty.java    From myapplication with Apache License 2.0 6 votes vote down vote up
private void initLoc() {
    //初始化定位
    mLocationClient = new AMapLocationClient(getApplicationContext());
    //设置定位回调监听
    mLocationClient.setLocationListener(this);
    //初始化定位参数
    mLocationOption = new AMapLocationClientOption();
    //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    //设置是否返回地址信息(默认返回地址信息)
    mLocationOption.setNeedAddress(true);
    //设置是否只定位一次,默认为false
    mLocationOption.setOnceLocation(true);
    //设置是否强制刷新WIFI,默认为强制刷新
    mLocationOption.setWifiActiveScan(true);
    //设置是否允许模拟位置,默认为false,不允许模拟位置
    mLocationOption.setMockEnable(false);
    //设置定位间隔,单位毫秒,默认为2000ms
    mLocationOption.setInterval(2000);
    //给定位客户端对象设置定位参数
    mLocationClient.setLocationOption(mLocationOption);
    //启动定位
    mLocationClient.startLocation();
}
 
Example 11
Source File: Location_Activity.java    From Android_Location_Demo with Apache License 2.0 6 votes vote down vote up
/**
 * 默认的定位参数
 * @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;
}
 
Example 12
Source File: SplashPresenter.java    From LittleFreshWeather with Apache License 2.0 6 votes vote down vote up
private void initLocation() {
    mLocationClient = new AMapLocationClient(mView.getContext().getApplicationContext());
    mLocationClientOption = new AMapLocationClientOption();
    //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
    mLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    //设置是否返回地址信息(默认返回地址信息)
    mLocationClientOption.setNeedAddress(true);
    //设置是否只定位一次,默认为false
    mLocationClientOption.setOnceLocation(false);
    //设置是否强制刷新WIFI,默认为强制刷新
    mLocationClientOption.setWifiActiveScan(true);
    //设置是否允许模拟位置,默认为false,不允许模拟位置
    mLocationClientOption.setMockEnable(true);
    //设置定位间隔,单位毫秒,默认为2000ms
    mLocationClientOption.setInterval(1000);
    mLocationClient.setLocationOption(mLocationClientOption);
    mLocationClient.setLocationListener(this);
}
 
Example 13
Source File: LocationManager.java    From Android-Application-ZJB with Apache License 2.0 6 votes vote down vote up
public void init() {
    //初始化定位参数
    mLocationOption = new AMapLocationClientOption();
    //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    //设置是否返回地址信息(默认返回地址信息)
    mLocationOption.setNeedAddress(true);
    //设置是否只定位一次,默认为false
    mLocationOption.setOnceLocation(false);
    //设置是否强制刷新WIFI,默认为强制刷新
    mLocationOption.setWifiActiveScan(true);
    //设置是否允许模拟位置,默认为false,不允许模拟位置
    mLocationOption.setMockEnable(false);
    //设置定位间隔,单位毫秒,默认为2000ms
    mLocationOption.setInterval(2000);
}
 
Example 14
Source File: MeEditorAreaAty.java    From myapplication with Apache License 2.0 6 votes vote down vote up
/**
 * 设置监听
 */
private void setUpListener() {
    titleLeftImv.setOnClickListener(this);
    currentAreaRout.setOnClickListener(this);

    fullAreaRout.setOnClickListener(this);
    // 添加change事件
    mViewProvince.addChangingListener(this);
    // 添加change事件
    mViewCity.addChangingListener(this);
    // 添加change事件
    mViewDistrict.addChangingListener(this);
    // 添加onclick事件
    mConfirmTv.setOnClickListener(this);

    // 设置高德地图
    aMapLocationClient = new AMapLocationClient(this.getApplicationContext());
    aMapLocationClientOption = new AMapLocationClientOption();
    aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    aMapLocationClientOption.setOnceLocation(true);
    aMapLocationClient.setLocationListener(this);
}
 
Example 15
Source File: MainFragment.java    From SeeWeather with Apache License 2.0 5 votes vote down vote up
/**
 * 高德定位
 */
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();
}
 
Example 16
Source File: InitAMapTask.java    From android-performance with MIT License 5 votes vote down vote up
@Override
public void run() {

    mLocationClient = new AMapLocationClient(mContext);
    mLocationClient.setLocationListener(mLocationListener);
    mLocationOption = new AMapLocationClientOption();
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
    mLocationOption.setOnceLocation(true);
    mLocationClient.setLocationOption(mLocationOption);
    mLocationClient.startLocation();
}
 
Example 17
Source File: PerformanceApp.java    From android-performance with MIT License 5 votes vote down vote up
private void initAMap() {
    mLocationClient = new AMapLocationClient(getApplicationContext());
    mLocationClient.setLocationListener(mLocationListener);
    mLocationOption = new AMapLocationClientOption();
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    mLocationOption.setOnceLocation(true);
    mLocationClient.setLocationOption(mLocationOption);
    mLocationClient.startLocation();
}
 
Example 18
Source File: FindWeatherAty.java    From myapplication with Apache License 2.0 4 votes vote down vote up
private void initViews() {
    isConnected = SystemUtils.checkNetworkConnection(this);

    titleLeftImv = (ImageButton) findViewById(R.id.title_left_imv);
    titleCityTv = (TextView) findViewById(R.id.title_center_tv);

    mCurDateTv = (TextView) findViewById(R.id.find_today_date);
    mCurTempTv = (TextView) findViewById(R.id.find_weather_temp);
    mCurPm25Tv = (TextView) findViewById(R.id.find_weather_pm25_tv);
    mCurWeaDesTv = (TextView) findViewById(R.id.find_weather_describe);
    mCurWeaWindTv = (TextView) findViewById(R.id.find_weather_wind);

    mTodayWeaImv = (ImageView) findViewById(R.id.find_weather_today_imv);
    mTodayWeaHighTmpTv = (TextView) findViewById(R.id.find_weather_today_hightmp);
    mTodayWeaLowTmpTv = (TextView) findViewById(R.id.find_weather_today_lowtmp);
    mTodayWeaDesTv = (TextView) findViewById(R.id.find_weather_today_wind_tv);

    mTomorrowWeaImv = (ImageView) findViewById(R.id.find_weather_tomorrow_imv);
    mTomorrowWeaHighTmpTv = (TextView) findViewById(R.id.find_weather_tomorrow_hightmp);
    mTomorrowWeaLowTmpTv = (TextView) findViewById(R.id.find_weather_tomorrow_lowtmp);
    mTomorrowWeaDesTv = (TextView) findViewById(R.id.find_weather_tomorrow_wind_tv);

    mData1Imv = (ImageView) findViewById(R.id.find_data1_imv);
    mData1WeaDesTv = (TextView) findViewById(R.id.find_data1_tmp_tv);
    mData1WeaWinsTv = (TextView) findViewById(R.id.find_data1_wind_tv);

    mData2TimeTv = (TextView) findViewById(R.id.find_data2_title_tv);
    mData2Imv = (ImageView) findViewById(R.id.find_data2_imv);
    mData2WeaDesTv = (TextView) findViewById(R.id.find_data2_tmp_tv);
    mData2WeaWinsTv = (TextView) findViewById(R.id.find_data2_wind_tv);

    mData3TimeTv = (TextView) findViewById(R.id.find_data3_title_tv);
    mData3Imv = (ImageView) findViewById(R.id.find_data3_imv);
    mData3WeaDesTv = (TextView) findViewById(R.id.find_data3_tmp_tv);
    mData3WeaWinsTv = (TextView) findViewById(R.id.find_data3_wind_tv);

    mData4TimeTv = (TextView) findViewById(R.id.find_data4_title_tv);
    mData4Imv = (ImageView) findViewById(R.id.find_data4_imv);
    mData4WeaDesTv = (TextView) findViewById(R.id.find_data4_tmp_tv);
    mData4WeaWinsTv = (TextView) findViewById(R.id.find_data4_wind_tv);

    mWeatherChartView = (WeatherChartView) findViewById(R.id.find_weather_weather_chart);

    mDressRout = (RelativeLayout) findViewById(R.id.find_dress_rout);
    mCarRout = (RelativeLayout) findViewById(R.id.find_car_rout);
    mTripRout = (RelativeLayout) findViewById(R.id.find_trip_rout);
    mColdlRout = (RelativeLayout) findViewById(R.id.find_coldl_rout);
    mSportRout = (RelativeLayout) findViewById(R.id.find_sport_rout);
    mSunRout = (RelativeLayout) findViewById(R.id.find_ziwaixian_rout);

    mDressTv = (TextView) findViewById(R.id.find_dress_tv);
    mDressTipTv = (TextView) findViewById(R.id.find_dress_tip_tv);

    mCarTv = (TextView) findViewById(R.id.find_car_tv);
    mCarTipTv = (TextView) findViewById(R.id.find_car_tip_tv);

    mTripTv = (TextView) findViewById(R.id.find_trip_tv);
    mTripTipTv = (TextView) findViewById(R.id.find_trip_tip_tv);

    mColdlTv = (TextView) findViewById(R.id.find_coldl_tv);
    mColdlTipTv = (TextView) findViewById(R.id.find_coldl_tip_tv);

    mSportTv = (TextView) findViewById(R.id.find_sport_tv);
    mSportTipTv = (TextView) findViewById(R.id.find_sport_tip_tv);

    mSunTv = (TextView) findViewById(R.id.find_ziwaixian_tv);
    mSunTipTv = (TextView) findViewById(R.id.find_ziwaixian_tip_tv);

    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.find_weather_SRfLout);
    mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,
            R.color.colorPrimary,
            R.color.teal);

    // 设置高德地图
    aMapLocationClient = new AMapLocationClient(FindWeatherAty.this);
    aMapLocationClientOption = new AMapLocationClientOption();
    aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    aMapLocationClientOption.setOnceLocation(true);
    aMapLocationClient.setLocationListener(this);

    // 进入页面mSwipeRefreshLayout自动刷新并填充数据
    mSwipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);

            if (isConnected) {
                aMapLocationClientOption.setNeedAddress(true);
                aMapLocationClientOption.setInterval(1000);
                aMapLocationClient.setLocationOption(aMapLocationClientOption);
                aMapLocationClient.startLocation();
                mLocationHandler.sendEmptyMessage(AMapUtils.MSG_LOCATION_START);
            } else {
                SystemUtils.noNetworkAlert(FindWeatherAty.this);
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }
    });
}
 
Example 19
Source File: AMapLocationReactModule.java    From react-native-amap-location with MIT License 4 votes vote down vote up
@ReactMethod
public void startLocation(@Nullable ReadableMap options) {
    this.mLocationClient.startLocation();
    AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
    // 默认值
    needMars = false;
    needDetail = false;
    if (options != null) {
        if (options.hasKey("needMars")) {
            needMars = options.getBoolean("needMars");
        }
        if (options.hasKey("needDetail")) {
            needDetail = options.getBoolean("needDetail");
        }
        if (options.hasKey("accuracy")) {
            //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
            switch(options.getString("accuracy")) {
                case "BatterySaving":
                    mLocationOption.setLocationMode(AMapLocationMode.Battery_Saving);
                    break;
                case "DeviceSensors":
                    mLocationOption.setLocationMode(AMapLocationMode.Device_Sensors);
                    break;
                case "HighAccuracy":
                    mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
                    break;
                default:
                    break;
            }
        }
        if (options.hasKey("needAddress")) {
            //设置是否返回地址信息(默认返回地址信息)
            mLocationOption.setNeedAddress(options.getBoolean("needAddress"));
        }
        if (options.hasKey("onceLocation")) {
            //设置是否只定位一次,默认为false
            mLocationOption.setOnceLocation(options.getBoolean("onceLocation"));
        }
        if (options.hasKey("wifiActiveScan")) {
            //设置是否强制刷新WIFI,默认为强制刷新
            //模式为仅设备模式(Device_Sensors)时无效
            mLocationOption.setWifiActiveScan(options.getBoolean("wifiActiveScan"));
        }
        if (options.hasKey("mockEnable")) {
            //设置是否允许模拟位置,默认为false,不允许模拟位置
            //模式为低功耗模式(Battery_Saving)时无效
            mLocationOption.setMockEnable(options.getBoolean("mockEnable"));
        }
        if (options.hasKey("interval")) {
            //设置定位间隔,单位毫秒,默认为2000ms
            mLocationOption.setInterval(options.getInt("interval"));
        }
        if (options.hasKey("killProcess")) {
            //设置退出时是否杀死service
            //默认值:false, 不杀死
            //模式为仅设备模式(Device_Sensors)时无效
            mLocationOption.setKillProcess(options.getBoolean("killProcess"));
        }

        if (options.hasKey("httpTimeOut")) {
            //设置联网超时时间
            //默认值:30000毫秒
            //模式为仅设备模式(Device_Sensors)时无效
            mLocationOption.setHttpTimeOut(options.getInt("httpTimeOut"));
        }
    }

    //给定位客户端对象设置定位参数
    this.mLocationClient.setLocationOption(mLocationOption);
    //启动定位
    this.mLocationClient.startLocation();
}
 
Example 20
Source File: GeoFence_Old_Activity.java    From Android_Location_Demo with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_geofence_old);
	setTitle(R.string.oldGeoFence);

	etRadius = (EditText) findViewById(R.id.et_radius);
	cbAlertIn = (CheckBox) findViewById(R.id.cb_alertIn);
	cbAlertOut = (CheckBox) findViewById(R.id.cb_alertOut);
	tvReult = (TextView) findViewById(R.id.tv_result);
	btFence = (Button) findViewById(R.id.bt_fence);

	btFence.setOnClickListener(this);

	IntentFilter fliter = new IntentFilter(
			ConnectivityManager.CONNECTIVITY_ACTION);
	fliter.addAction(GEOFENCE_BROADCAST_ACTION);
	registerReceiver(mGeoFenceReceiver, fliter);
	Intent intent = new Intent(GEOFENCE_BROADCAST_ACTION);
	mPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
			intent, 0);
	onceClient = new AMapLocationClient(getApplicationContext());
	locationClient = new AMapLocationClient(this.getApplicationContext());
	locationOption = new AMapLocationClientOption();

	// 设置定位模式高精度,添加地理围栏最好设置成高精度模式
	locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
	// 设置定位监听
	locationClient.setLocationListener(this);

	AMapLocationClientOption onceOption = new AMapLocationClientOption();
	onceOption.setOnceLocation(true);
	onceClient.setLocationOption(onceOption);
	onceClient.setLocationListener(new AMapLocationListener() {
		@Override
		public void onLocationChanged(AMapLocation loc) {
			if (loc != null) {
				if (loc.getErrorCode() == 0) {
					if (null != locationClient) {
						float radius = 1000;
						String strRadius = etRadius.getText().toString();
						if (!TextUtils.isEmpty(strRadius)) {
							radius = Float.valueOf(strRadius);
						}
						
						// 添加地理围栏,
						// 第一个参数:围栏ID,可以自定义ID,示例中为了方便只使用一个ID;第二个:纬度;第三个:精度;
						// 第四个:半径;第五个:过期时间,单位毫秒,-1代表不过期;第六个:接收触发消息的PendingIntent
						locationClient.addGeoFenceAlert("fenceId",
								loc.getLatitude(), loc.getLongitude(),
								radius, -1, mPendingIntent);
					}
				} else {
					Toast.makeText(getApplicationContext(), "获取当前位置失败!",
							Toast.LENGTH_SHORT).show();
					
					Message msg = mHandler.obtainMessage();
					msg.obj = loc;
					msg.what = -1;
					mHandler.sendMessage(msg);
				}
			}
		}
	});
}