Android–使用手机GPS获取经纬度

                        标签:          gps经纬度                                3810人阅读         评论(5)         收藏          举报        本文章已收录于:                         分类:                                                                           

有很多应用在打开应用的时候,就需要去获取当前手机的地址位置并且显示出地名等,现在介绍一种方法通过手机内置的GPS获取当前位置的经纬度.

思路: 首先判断手机当前是否打开GPS功能–>如果没有打开就通过代码帮助用户强制打开手机GPS功能–>根据手机GPS获取手机当前位置的经纬度.

注意:打开gps和访问网络的权限.

1.上代码:

1.1判断手机是否打开GPS-需要使用到Android系统的LocationManager对象.

/** * 判断手机GPS是否开启 * @param mainActivity * @return */public boolean isOpen(Context context) {    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);    //通过GPS卫星定位,定位级别到街    boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);    //通过WLAN或者移动网络确定位置    boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);    if (gps || network) {        return true;    }    return false;}

1.2用户未打开GPS,帮助用户打开该功能.

/** * 开启手机GPS */public void openGPS(Context context) {    Intent GPSIntent = new Intent();    GPSIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvide");    GPSIntent.addCategory("android.intent.category.ALTERNATIVE");    GPSIntent.setData(Uri.parse("custom:3"));    try {        //使用PendingIntent发送广播告诉手机去开启GPS功能        PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();    } catch (PendingIntent.CanceledException e) {        e.printStackTrace();    }}

1.3使用手机的GPS功能获取当前位置的经纬度

/** * GPS功能已经打开-->根据GPS去获取经纬度 */public void getGPSConfi() {    Location location;    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);    } else {        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);    }    if (location != null) {        double latitude = location.getLatitude();        double longitude = location.getLongitude();        tv_content.setText("经纬度:" + latitude + "--" + longitude);    } else {        tv_content.setText("未获取到经纬度数据");    }}

2.需要的权限:

android:name="android.permission.ACCESS_COARSE_LOCATION" />android:name="android.permission.ACCESS_FINE_LOCATION" />android:name="android.permission.ACCESS_WIFI_STATE" />android:name="android.permission.ACCESS_NETWORK_STATE" />android:name="android.permission.CHANGE_WIFI_STATE" />android:name="android.permission.INTERNET" />
3.界面很简单,就不贴代码了,一个按钮和一个文本框显示经纬度




document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000) var fromjs = ("#fromjs");
    if (fromjs.length > 0) {
("#fromjs .markdown_views pre").addClass("prettyprint"); prettyPrint(); ('pre.prettyprint code').each(function () {
                var lines =
(this).text().split('\n').length; var numbering= ('
    ').addClass('pre-numbering').hide(); (this).addClass(hasnumbering).parent().append( numbering); for (i = 1; i

更多相关文章

  1. android上代码去执行"su"命令
  2. android自动关机代码
  3. Android(安卓)实现微信,微博,微信朋友圈,QQ分享的功能
  4. android 杂记-- adb 使用笔记
  5. Android: 如何利用Handler处理和发送消息来实现页面刷新的功能
  6. android中sd卡常用状态解析
  7. JS判断是否是微信页面,判断手机操作系统(ios或android)并跳转到不同
  8. 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveI
  9. Android平台开发-Bluez function porting-蓝牙功能移植

随机推荐

  1. android英语字典(内含源码哦)
  2. android 模拟器启动不了
  3. Android manifest之系统自带的permission
  4. Flutter之原生交互(Android)
  5. Android系列学习:handler,HandlerThread
  6. Android-View的滑动
  7. [Android]ScrollView和ListView套用冲突
  8. 狂刷Android范例之2:剪贴板范例
  9. 转:深入解读Linux与Android的相互关系
  10. Android之用自定义的shape去实现shadow效