培训网站制作网站,python基础教程题库,网站怎么销售,五常市网站文章目录 Android 系统定位和高德定位系统定位工具类封装LocationManager使用 高德定位封装高德地图使用 Android 系统定位和高德定位
系统定位
工具类
public class LocationUtils {public static final int REQUEST_LOCATION 0xa1;/*** 判断定位服务是否开启*/public sta… 文章目录 Android 系统定位和高德定位系统定位工具类封装LocationManager使用 高德定位封装高德地图使用 Android 系统定位和高德定位
系统定位
工具类
public class LocationUtils {public static final int REQUEST_LOCATION 0xa1;/*** 判断定位服务是否开启*/public static boolean isOpenLocationServer(Context context) {int locationMode 0;try {locationMode Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);} catch (Settings.SettingNotFoundException e) {e.printStackTrace();return false;}return locationMode ! Settings.Secure.LOCATION_MODE_OFF;}/*** 判断GPS是否可用*/public static boolean isGPSEnabled(Context context) {LocationManager manager (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);return manager.isProviderEnabled(LocationManager.GPS_PROVIDER);}/*** 判断定位是否可用*/public static boolean isLocationEnabled(Context context) {LocationManager manager (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);return manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) || manager.isProviderEnabled(LocationManager.GPS_PROVIDER);}/*** 开启位置服务*/public static void openLocation(Activity activity) {activity.startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), REQUEST_LOCATION);}/*** 开启位置服务*/public static void openLocation(Fragment fragment) {fragment.startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), REQUEST_LOCATION);}}封装LocationManager
public class LocationHelper {private static OnLocationChangeListener mOnLocationChangeListener;private static MyLocationListener mLocationListener;private static LocationManager mLocationManager;private static String mProvider;/*** 注册*/public static void registerLocation(Context context, Nullable OnLocationChangeListener listener) {mOnLocationChangeListener listener;mLocationManager (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);mProvider mLocationManager.getBestProvider(getCriteria(), true);Location lastKnownLocation mLocationManager.getLastKnownLocation(mProvider);if (listener ! null lastKnownLocation ! null) {listener.onLastKnownLocation(lastKnownLocation);}mLocationListener new MyLocationListener();startLocation();}/*** 开始定位*/public static void startLocation() {// 第二个参数更新定位的时间间隔第三个参数更新定位的距离范围mLocationManager.requestLocationUpdates(mProvider, 0, 0, mLocationListener);}/*** 停止定位*/public static void stopLocation() {mLocationManager.removeUpdates(mLocationListener);}/*** 设置定位参数** return {link Criteria}*/private static Criteria getCriteria() {Criteria criteria new Criteria();// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略Criteria.ACCURACY_FINE则比较精细criteria.setAccuracy(Criteria.ACCURACY_FINE);// 设置是否要求速度criteria.setSpeedRequired(false);// 设置是否允许运营商收费criteria.setCostAllowed(false);// 设置是否需要方位信息criteria.setBearingRequired(false);// 设置是否需要海拔信息criteria.setAltitudeRequired(false);// 设置对电源的需求criteria.setPowerRequirement(Criteria.POWER_LOW);return criteria;}/*** 取消注册*/public static void unregisterLocation() {if (mLocationManager ! null) {if (mLocationListener ! null) {mLocationManager.removeUpdates(mLocationListener);}}mLocationManager null;mLocationListener null;mOnLocationChangeListener null;}public interface OnLocationChangeListener {void onLastKnownLocation(Location location);void onLocationChanged(Location location);}public static class MyLocationListener implements LocationListener {Overridepublic void onLocationChanged(NonNull Location location) {if (mOnLocationChangeListener ! null) {mOnLocationChangeListener.onLocationChanged(location);}}}
}public class LocationService extends Service {public static void actionStart(Context context) {context.startService(new Intent(context, LocationService.class));}public static void actionStop(Context context) {context.stopService(new Intent(context, LocationService.class));}SuppressLint(MissingPermission)Overridepublic void onCreate() {super.onCreate();new Thread(() - {Looper.prepare();LocationHelper.registerLocation(getApplicationContext(), new LocationHelper.OnLocationChangeListener() {Overridepublic void onLastKnownLocation(Location location) {if (location ! null) {Log.e(TAG, onLastKnownLocation location);}}Overridepublic void onLocationChanged(Location location) {if (location ! null) {Log.e(TAG, onLocationChanged location);stopSelf();}}});Looper.loop();}).start();}NullableOverridepublic IBinder onBind(Intent intent) {return null;}Overridepublic void onDestroy() {super.onDestroy();LocationHelper.unregisterLocation();}
}使用
// 开始定位
LocationService.actionStart(MainActivity.this, start);// 停止定位
LocationService.actionStart(MainActivity.this, stop);// 单次定位
LocationService.actionStart(MainActivity.this, once);高德定位
封装高德地图
public class GDLocationHelper {private static AMapLocationClient locationClient;private static OnLocationChangeListener mOnLocationChangeListener;private static MyLocationListener mListener;public static void register(Context context, Nullable OnLocationChangeListener onLocationChangeListener) {mOnLocationChangeListener onLocationChangeListener;initLocation(context);}private static void initLocation(Context context) {locationClient new AMapLocationClient(context);AMapLocation lastKnownLocation locationClient.getLastKnownLocation();if (lastKnownLocation ! null mOnLocationChangeListener ! null) {mOnLocationChangeListener.onLastKnownLocation(lastKnownLocation);}// 设置定位监听mListener new MyLocationListener();locationClient.setLocationListener(mListener);}public static void startLocation() {locationClient.stopLocation();locationClient.setLocationOption(getDefaultOption(false));locationClient.startLocation();}public static void startOnceLocation() {locationClient.stopLocation();locationClient.setLocationOption(getDefaultOption(true));locationClient.startLocation();}public static void stopLocation() {locationClient.stopLocation();}public static void unregister() {stopLocation();locationClient.onDestroy();locationClient null;mOnLocationChangeListener null;mListener null;}public static AMapLocationClientOption getDefaultOption(boolean isOnce) {AMapLocationClientOption mOption new AMapLocationClientOption();mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选设置定位模式可选的模式有高精度、仅设备、仅网络。默认为高精度模式
// mOption.setGpsFirst(false);//可选设置是否gps优先只在高精度模式下有效。默认关闭
// mOption.setHttpTimeOut(30000);//可选设置网络请求超时时间。默认为30秒。在仅设备模式下无效
// mOption.setNeedAddress(true);//可选设置是否返回逆地理地址信息。默认是trueif (isOnce) {mOption.setOnceLocation(true);//可选设置是否单次定位。默认是falsemOption.setOnceLocationLatest(true);//可选设置是否等待wifi刷新默认为false.如果设置为true,会自动变为单次定位持续定位时不要使用} else {mOption.setOnceLocation(false);mOption.setInterval(2_000);//可选设置定位间隔。默认为2秒}mOption.setMockEnable(false); //设置是否允许模拟位置
// AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
// mOption.setSensorEnable(false);//可选设置是否使用传感器。默认是falsemOption.setWifiScan(true); //可选设置是否开启wifi扫描。默认为true如果设置为false会同时停止主动刷新停止以后完全依赖于系统刷新定位位置可能存在误差mOption.setLocationCacheEnable(true); //可选设置是否使用缓存定位默认为true
// mOption.setGeoLanguage(AMapLocationClientOption.GeoLanguage.DEFAULT);//可选设置逆地理信息的语言默认值为默认语言根据所在地区选择语言return mOption;}public static class MyLocationListener implements AMapLocationListener {Overridepublic void onLocationChanged(AMapLocation aMapLocation) {if (mOnLocationChangeListener ! null) {mOnLocationChangeListener.onLocationChanged(aMapLocation);}}}public interface OnLocationChangeListener {void onLastKnownLocation(AMapLocation location);void onLocationChanged(AMapLocation location);}
}public class GDLocationService extends Service {private volatile boolean isOnce false;public static void actionStart(Context context) {actionStart(context, null);}public static void actionStart(Context context, String command) {context.startService(new Intent(context, GDLocationService.class).putExtra(command, command));}public static void actionStop(Context context) {context.stopService(new Intent(context, GDLocationService.class));}Overridepublic void onCreate() {super.onCreate();GDLocationHelper.register(getApplicationContext(), new GDLocationHelper.OnLocationChangeListener() {Overridepublic void onLastKnownLocation(AMapLocation location) {if (location ! null) {Log.e(TAG, onLastKnownLocation location.toString());}}Overridepublic void onLocationChanged(AMapLocation location) {if (location ! null) {Log.e(TAG, onLocationChanged location.toString());if (isOnce) {stopSelf();}}}});}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {String command intent.getStringExtra(command);switch (command) {case once:isOnce true;GDLocationHelper.startOnceLocation();break;case stop:GDLocationHelper.stopLocation();break;case start:default:isOnce false;GDLocationHelper.startLocation();break;}return super.onStartCommand(intent, flags, startId);}NullableOverridepublic IBinder onBind(Intent intent) {return null;}Overridepublic void onDestroy() {super.onDestroy();GDLocationHelper.unregister();Log.e(TAG, onDestroy);}
}使用
// 开始定位
GDLocationService.actionStart(MainActivity.this, start);// 停止定位
GDLocationService.actionStart(MainActivity.this, stop);// 单次定位
GDLocationService.actionStart(MainActivity.this, once);
文章转载自: http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.brld.cn.gov.cn.brld.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.lxlfr.cn.gov.cn.lxlfr.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.tdnbw.cn.gov.cn.tdnbw.cn http://www.morning.dhpjq.cn.gov.cn.dhpjq.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.rhmt.cn.gov.cn.rhmt.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn http://www.morning.rxrw.cn.gov.cn.rxrw.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.ycnqk.cn.gov.cn.ycnqk.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.bgzgq.cn.gov.cn.bgzgq.cn http://www.morning.krswn.cn.gov.cn.krswn.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.rfjmy.cn.gov.cn.rfjmy.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.pngdc.cn.gov.cn.pngdc.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.qwqzk.cn.gov.cn.qwqzk.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.dnvhfh.cn.gov.cn.dnvhfh.cn http://www.morning.lwwnq.cn.gov.cn.lwwnq.cn http://www.morning.jqjnx.cn.gov.cn.jqjnx.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.nfpct.cn.gov.cn.nfpct.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.czzpm.cn.gov.cn.czzpm.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.yrjkz.cn.gov.cn.yrjkz.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.qnbgh.cn.gov.cn.qnbgh.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn