com.amap.api.location.AMapLocationClient Java Examples

The following examples show how to use com.amap.api.location.AMapLocationClient. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #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: LocationService.java    From OpenWeatherPlus-Android with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {

    super.onCreate();

    //初始化定位
    mLocationClient = new AMapLocationClient(getApplicationContext());
    //声明AMapLocationClientOption对象
    AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
    //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
    mLocationOption.setInterval(10000);
    //单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
    mLocationOption.setHttpTimeOut(20000);
    mLocationClient.setLocationListener(mLocationListener);
    //给定位客户端对象设置定位参数
    mLocationClient.setLocationOption(mLocationOption);
    //启动定位
    mLocationClient.startLocation();
}
 
Example #10
Source File: MainActivity.java    From OpenWeatherPlus-Android with Apache License 2.0 6 votes vote down vote up
private void initLocation() {
    //初始化定位
    mLocationClient = new AMapLocationClient(getApplicationContext());
    //设置定位回调监听

    //声明AMapLocationClientOption对象
    AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
    //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
    mLocationOption.setInterval(10000);
    //单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
    mLocationOption.setHttpTimeOut(20000);
    mLocationClient.setLocationListener(mLocationListener);
    //给定位客户端对象设置定位参数
    mLocationClient.setLocationOption(mLocationOption);
    //启动定位
    mLocationClient.startLocation();
}
 
Example #11
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 #12
Source File: Location_Activity.java    From Android_Location_Demo with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化定位
 * 
 * @since 2.8.0
 * @author hongming.wang
 *
 */
private void initLocation(){
	//初始化client
	locationClient = new AMapLocationClient(this.getApplicationContext());
	//设置定位参数
	locationClient.setLocationOption(getDefaultOption());
	// 设置定位监听
	locationClient.setLocationListener(locationListener);
}
 
Example #13
Source File: AMapLocationReactModule.java    From react-native-amap-location with MIT License 5 votes vote down vote up
public AMapLocationReactModule(ReactApplicationContext reactContext) {
    super(reactContext);
    //初始化定位
    this.mLocationClient = new AMapLocationClient(reactContext);
    //设置定位回调监听
    this.mLocationClient.setLocationListener(mLocationListener);
    mReactContext = reactContext;
}
 
Example #14
Source File: LastLocation_Activity.java    From Android_Location_Demo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_lastlocation);
	setTitle(R.string.title_lastLocation);
	tvReult = (TextView) findViewById(R.id.tv_result);
	btnLastLoc = (Button) findViewById(R.id.bt_lastLoc);
	btnLastLoc.setOnClickListener(this);
	locationClient = new AMapLocationClient(this.getApplicationContext());
}
 
Example #15
Source File: Alarm_Location_Activity.java    From Android_Location_Demo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_alarm_location);
	setTitle(R.string.title_alarmCPU);

	etInterval = (EditText) findViewById(R.id.et_interval);
	etAlarm = (EditText) findViewById(R.id.et_alarm);
	cbAddress = (CheckBox) findViewById(R.id.cb_needAddress);
	cbGpsFirst = (CheckBox) findViewById(R.id.cb_gpsFirst);
	tvReult = (TextView) findViewById(R.id.tv_result);
	btLocation = (Button) findViewById(R.id.bt_location);

	btLocation.setOnClickListener(this);

	locationClient = new AMapLocationClient(this.getApplicationContext());
	locationOption = new AMapLocationClientOption();
	// 设置定位模式为高精度模式
	locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
	// 设置定位监听
	locationClient.setLocationListener(this);
	
	// 创建Intent对象,action为LOCATION
	alarmIntent = new Intent();
	alarmIntent.setAction("LOCATION");
	IntentFilter ift = new IntentFilter();

	// 定义一个PendingIntent对象,PendingIntent.getBroadcast包含了sendBroadcast的动作。
	// 也就是发送了action 为"LOCATION"的intent
	alarmPi = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
	// AlarmManager对象,注意这里并不是new一个对象,Alarmmanager为系统级服务
	alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
	
	//动态注册一个广播
	IntentFilter filter = new IntentFilter();
	filter.addAction("LOCATION");
	registerReceiver(alarmReceiver, filter);
}
 
Example #16
Source File: Alarm_Location_Activity.java    From Android_Location_Demo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_alarm_location);
	setTitle(R.string.title_alarmCPU);

	etInterval = (EditText) findViewById(R.id.et_interval);
	etAlarm = (EditText) findViewById(R.id.et_alarm);
	cbAddress = (CheckBox) findViewById(R.id.cb_needAddress);
	cbGpsFirst = (CheckBox) findViewById(R.id.cb_gpsFirst);
	tvReult = (TextView) findViewById(R.id.tv_result);
	btLocation = (Button) findViewById(R.id.bt_location);

	btLocation.setOnClickListener(this);

	locationClient = new AMapLocationClient(this.getApplicationContext());
	locationOption = new AMapLocationClientOption();
	// 设置定位模式为高精度模式
	locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
	// 设置定位监听
	locationClient.setLocationListener(this);
	
	// 创建Intent对象,action为LOCATION
	alarmIntent = new Intent();
	alarmIntent.setAction("LOCATION");
	IntentFilter ift = new IntentFilter();

	// 定义一个PendingIntent对象,PendingIntent.getBroadcast包含了sendBroadcast的动作。
	// 也就是发送了action 为"LOCATION"的intent
	alarmPi = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
	// AlarmManager对象,注意这里并不是new一个对象,Alarmmanager为系统级服务
	alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
	
	//动态注册一个广播
	IntentFilter filter = new IntentFilter();
	filter.addAction("LOCATION");
	registerReceiver(alarmReceiver, filter);
}
 
Example #17
Source File: Assistant_Location_Activity.java    From Android_Location_Demo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_assistant_location);
	setTitle(R.string.title_assistantLocation);

	btAssistant = (Button) findViewById(R.id.bt_assistant);
	btLocation = (Button) findViewById(R.id.bt_location);

	locationClient = new AMapLocationClient(getApplicationContext());
	btAssistant.setOnClickListener(this);
	btLocation.setOnClickListener(this);

	initWebView();
}
 
Example #18
Source File: Location_Activity.java    From Android_Location_Demo with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化定位
 * 
 * @since 2.8.0
 * @author hongming.wang
 *
 */
private void initLocation(){
	//初始化client
	locationClient = new AMapLocationClient(this.getApplicationContext());
	//设置定位参数
	locationClient.setLocationOption(getDefaultOption());
	// 设置定位监听
	locationClient.setLocationListener(locationListener);
}
 
Example #19
Source File: LastLocation_Activity.java    From Android_Location_Demo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_lastlocation);
	setTitle(R.string.title_lastLocation);
	tvReult = (TextView) findViewById(R.id.tv_result);
	btnLastLoc = (Button) findViewById(R.id.bt_lastLoc);
	btnLastLoc.setOnClickListener(this);
	locationClient = new AMapLocationClient(this.getApplicationContext());
}
 
Example #20
Source File: Assistant_Location_Activity.java    From Android_Location_Demo with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_assistant_location);
	setTitle(R.string.title_assistantLocation);

	btAssistant = (Button) findViewById(R.id.bt_assistant);
	btLocation = (Button) findViewById(R.id.bt_location);

	locationClient = new AMapLocationClient(getApplicationContext());
	btAssistant.setOnClickListener(this);
	btLocation.setOnClickListener(this);

	initWebView();
}
 
Example #21
Source File: AMapLocationTool.java    From letv with Apache License 2.0 5 votes vote down vote up
private AMapLocationTool() {
    this.mLocationClient = null;
    this.locationOption = null;
    this.myListener = new MyLocationListenner(this);
    this.mHandler = new Handler();
    this.isStart = false;
    this.mLocationClient = new AMapLocationClient(BaseApplication.getInstance().getBaseContext());
}
 
Example #22
Source File: InitHelper.java    From AndroidAll with Apache License 2.0 5 votes vote down vote up
public static void initAMap(Context context) {
    AMapLocationClient mLocationClient = new AMapLocationClient(context.getApplicationContext());
    mLocationClient.setLocationListener(new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            // 一些处理
        }
    });
    AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    mLocationOption.setOnceLocation(true);
    mLocationClient.setLocationOption(mLocationOption);
    mLocationClient.startLocation();
}
 
Example #23
Source File: AmapLocationPlugin.java    From location_plugin with Apache License 2.0 5 votes vote down vote up
AmapLocationPlugin(Registrar registrar) {
	this.registrar = registrar;
	//初始化定位
	mLocationClient = new AMapLocationClient(registrar.context());

	//设置定位回调监听
	mLocationClient.setLocationListener(mAMapLocationListener);
}
 
Example #24
Source File: GaodeMapLocationManager.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public void activate(OnLocationChangedListener listener) {
    this.mListener = listener;
    if (this.mlocationClient == null) {
        Log.i("zdy", "activate");
        this.mlocationClient = new AMapLocationClient(this.context);
        this.mLocationOption = new AMapLocationClientOption();
        this.mlocationClient.setLocationListener(this);
        this.mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
        this.mLocationOption.setInterval(2000);
        this.mlocationClient.setLocationOption(this.mLocationOption);
        this.mlocationClient.startLocation();
    }
}
 
Example #25
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 #26
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 #27
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);
				}
			}
		}
	});
}
 
Example #28
Source File: FlutterAMapLocationRegister.java    From flutter_amap_plugin with MIT License 4 votes vote down vote up
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
    mLocationClient = new AMapLocationClient(FlutterAmapPlugin.registrar.activity().getApplicationContext());
}
 
Example #29
Source File: AmapWrapper.java    From RunMap with Apache License 2.0 4 votes vote down vote up
private void initLocationClient() {
    mAmapLocationClient = new AMapLocationClient(GlobalApplication.getAppContext());
    mAmapLocationClient.setLocationListener(mAmapStateListener);
    mAmapLocationClient.setLocationOption(mAmapLocationOption);
}
 
Example #30
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);
				}
			}
		}
	});
}