场景

根据GPS获取经纬度效果

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

修改页面布局代码activity_main.xml,在页面上添加一个TextView来显示经纬度信息。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/location"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:textStyle="bold" />
</RelativeLayout>

然后打开MainActivity.java,修改代码如下

package com.badao.servicetest;import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView text;  //定义用于显示LocationProvider的TextView组件@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);   //设置全屏显示text = (TextView) findViewById(R.id.location);  //获取显示Location信息的TextView组件//获取系统的LocationManager对象LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);//添加权限检查if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {// TODO: Consider calling//    ActivityCompat#requestPermissions// here to request the missing permissions, and then overriding//   public void onRequestPermissionsResult(int requestCode, String[] permissions,//                                          int[] grantResults)// to handle the case where the user grants the permission. See the documentation// for ActivityCompat#requestPermissions for more details.return;}//设置每一秒获取一次location信息locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,      //GPS定位提供者1000,       //更新数据时间为1秒1,      //位置间隔为1米//位置监听器new LocationListener() {  //GPS定位信息发生改变时触发,用于更新位置信息@Overridepublic void onLocationChanged(Location location) {//GPS信息发生改变时,更新位置locationUpdates(location);}@Override//位置状态发生改变时触发public void onStatusChanged(String provider, int status, Bundle extras) {}@Override//定位提供者启动时触发public void onProviderEnabled(String provider) {}@Override//定位提供者关闭时触发public void onProviderDisabled(String provider) {}});//从GPS获取最新的定位信息Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);locationUpdates(location);    //将最新的定位信息传递给创建的locationUpdates()方法中}public void locationUpdates(Location location) {  //获取指定的查询信息//如果location不为空时if (location != null) {StringBuilder stringBuilder = new StringBuilder();        //使用StringBuilder保存数据//获取经度、纬度、等属性值stringBuilder.append("您的位置信息:\n");stringBuilder.append("经度:");stringBuilder.append(location.getLongitude());stringBuilder.append("\n纬度:");stringBuilder.append(location.getLatitude());
//            stringBuilder.append("\n精确度:");
//            stringBuilder.append(location.getAccuracy());
//            stringBuilder.append("\n高度:");
//            stringBuilder.append(location.getAltitude());
//            stringBuilder.append("\n方向:");
//            stringBuilder.append(location.getBearing());
//            stringBuilder.append("\n速度:");
//            stringBuilder.append(location.getSpeed());
//            stringBuilder.append("\n时间:");
//            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH mm ss");    //设置日期时间格式
//            stringBuilder.append(dateFormat.format(new Date(location.getTime())));text.setText(stringBuilder);            //显示获取的信息} else {//否则输出空信息text.setText("没有获取到GPS信息");}}
}

最后打开AndroidMainfest.xml添加权限

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

添加位置如下

Android中获取定位经纬度信息相关推荐

  1. android自动获取位置,Android中获取当前位置信息

    这篇教程主要介绍了在Android平台上如何使用服务完成定位功能.众所周知,Android设备的当前位置信息,对开发创新性App.解决人们日常生活问题有极大帮助.在Android平台开发定位相关的应用 ...

  2. STM32+安信可GP-01定位模块实现获取定位经纬度信息

    文章目录 GP-01模组简介 1.硬件准备 1.1 GP-01-Kit 1.2 STM32F103C8T6核心板或者最小开发板 1.3 接线方式 2.软件准备 2.1 MDK(Keil v5) 2.2 ...

  3. Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)

    本节内容主要是讲解ActivityManager的使用,通过ActivityManager我们可以获得系统里正在运行的activities,包括 进程(Process)等.应用程序/包.服务(Serv ...

  4. android 中获取logcat缓冲区信息

    很多时候,android程序难免会有很多运行时错误扑捉不到或者扑捉不充分,导致程序出错的时候记录不下错误. 如下图所示 此时最简单的就是,连接USB线,运行 adb logcat -b main -v ...

  5. 微信小程序整合高德地图获取定位经纬度信息

    1.登录高德地图https://lbs.amap.com/ a.创建新应用 这个key后边整合的时候会用到,精彩继续 b.下载SDK  https://lbs.amap.com/api/wx/down ...

  6. Android中获取手机设备信息、RAM、ROM存储信息,如宽、高、厂商名、手机品牌

    借鉴:https://www.jianshu.com/p/ca869aa2fd72 今天有两个工具类总结,代码里都有注释,直接看代码. 一.首先第一个,主要获取手机设备信息DeviceInfoUtil ...

  7. Android中获取手机电量信息

    有些时候我们需要在我们的应用上为用户展示当前手机的电量,这时候我们就需要用到广播了,我们都知道在动态注册广播的时候,我们需要传入一个BroadcastReceiver类对象,还有一个意图过滤器Inte ...

  8. 【Android App】GPS获取定位经纬度和根据经纬度获取详细地址讲解及实战(附源码和演示 超详细)

    需要全部代码请点赞关注收藏后评论区留言私信~~~ 一.获取定位信息 开启定位相关功能只是将定位的前提条件准备好,若想获得手机当前所处的位置信息,还要依靠下列的3种定位工具. (1)定位条件器Crite ...

  9. Vue Cli4 使用高德地图定位 获取当前经纬度信息以及周边定位

    以上是最终效果图 下面开始代码分享 第一步 在index,html引入高德地图模块 ` <!-- 高德地图 --><script type="text/javascript ...

最新文章

  1. codeforces3A
  2. 文本分类step by step(二)
  3. LWIP裸机环境下实现TCP与UDP通讯(转)
  4. Spring Boot 2.0(七):Spring Boot 如何解决项目启动时初始化资源
  5. 关于相对布局RelativeLayout的各种属性介绍
  6. linq To DataTable
  7. 国考计算机专业生报名人数,近四成岗位随便挑!2020年国考,这类专业招录人数接近一万...
  8. 在mysql中创建视图需要使用什么语句_mysql如何创建视图?创建语句是什么?
  9. mysql禁止明文密码_暂时在MySQL *中存储明文密码是否安全*?
  10. SRE Google运维解密——第二章Goolgle的生成环境介绍
  11. 【重磅】Crust主网进入节点接入阶段
  12. sam格式的结构和意义_sam格式详细说明
  13. python 爬虫《百炼成佛》爬虫入门 (爬虫介绍)第一个爬虫程序
  14. QQ2012如何恢复“合并会话窗口”为多个聊天窗口?
  15. 如何使用 CSS flex box 和 Javascript 设计棋盘
  16. 【spine】spine 简介
  17. 机器人导航必备的栅格地图数学模型及使用
  18. HDU 4507 吉哥系列故事——恨7不成妻 详解(变态数位DP)
  19. android dbflow引起内存泄漏,DBFlow使用说明(1)快速入门
  20. 译(四十四)-Python中Assert的作用

热门文章

  1. 王道计算机考研 数据结构 (查找-下)
  2. liunx系统中的盘符能修改嘛_装系统教程!如何从U盘启动(中)!小白也能变装机大神!...
  3. mysql查询索引like_通过索引查询慢速搜索LIKE%MYSQL
  4. python 打开网页 并填表单_Windows下使用python3 + selenium.webdriver功能实现自动填写网页表单功能...
  5. 线性时变系统能用模型预测控制吗_线性系统理论(二)运动分析
  6. win10 本地升级和系统覆盖更新教程
  7. eclipse 注释星号没对齐_36 个奇葩代码注释,看完笑哭了!
  8. win8网速怎么测试软件,Windows8小技巧:查看当前网速
  9. xgboost学习率不能大于1的原因
  10. php中类的构造函数是,php类与构造函数解析