Android开发之GPS定位获取位置
来源:互联网 发布:linux cp创建文件夹 编辑:程序博客网 时间:2024/05/21 08:20
下面代码实现了:
- 检测GPS是否开启
- 打开GPS应用
- 获取用户位置
添加权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" />
代码:
/** * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的 * @param context * @return true 表示开启 */public static final boolean isOPen(final Context context) {LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);// 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快)boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);// 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位)boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);if(gps==true){Log.e(TAG, "Test" + "GPS is open");}if(network==true){Log.e(TAG, "Test" + "network is open");}if (gps || network) {return true;}return false;}/* * Toast.makeText( this, “Please turn on GPS”, Toast.LENGTH_SHORT ).show();Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );startActivity(myIntent); *//** * 强制帮用户打开GPS * @param context */public static final void openGPS(Context context) {Intent GPSIntent = new Intent();GPSIntent.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");GPSIntent.addCategory("android.intent.category.ALTERNATIVE");GPSIntent.setData(Uri.parse("custom:3"));try {PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();} catch (CanceledException e) {e.printStackTrace();}} //Get the Location by GPS or WIFI public static Location getLocation(Context context) { LocationManager locMan = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); Location location = locMan .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null) { location = locMan .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } return location; } //get address from location public static Address getAddressbyLocation(Context context, Location location) { Address result = null; try { if (location != null) { Geocoder gc = new Geocoder(context, Locale.CHINA); double geoLatitude = location.getLatitude(); double geoLongitude = location.getLongitude(); List<Address> lstAddress = gc.getFromLocation(geoLatitude, geoLongitude, 1); if (lstAddress.size() > 0) { result = lstAddress.get(0); } } } catch (Exception e) { e.printStackTrace(); } return result; }
测试代码:
public static void test(Context context) {Log.e(TAG, "Test" + isOPen(context));Log.e(TAG, "Test" + getAddressbyLocation(context, getLocation(context)).getAddressLine(0));Log.e(TAG, "Test" + getAddressbyLocation(context, getLocation(context)).getLocality());}
0 0
- Android开发之GPS定位获取位置
- android 百度地图定位开发之时时定位-位置获取
- Android获取位置信息 - GPS定位 Network定位
- Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)
- Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)
- 安卓开发-Gps定位获取位置信息
- android 获取GPS定位,
- android GPS 获取位置
- android GPS 位置获取
- android开发之GPS定位详解
- android开发之GPS定位详解
- android开发之GPS定位详解
- android开发之GPS定位详解
- android开发之GPS定位详解
- android开发之GPS定位详解
- GPS定位获取位置,获取天气
- Android开发---GPS定位
- Android开发中定位解析(GPS定位、网络定位、位置地址解析)
- Qt 存储容器
- response.setHeader()的用法
- 大家的问题挑选回答
- 【LCA】 POJ 1330 Nearest Common Ancestors
- Android 之窗口小部件高级篇--App Widget 之 RemoteViews
- Android开发之GPS定位获取位置
- Android web与progressbar结合使用
- WinForm中利用DataSet向DataGridView中添加多行数据
- 程序的原子性
- Java深入 - Java 流
- Linux如何解决动态库的版本控制
- 非聚集索引结构
- mysql查询字段值为数字
- 3_C# 实现VMS客户端——使用log4net 写日志