Android中获取定位经纬度信息
16lz
2021-01-26
场景
根据GPS获取经纬度效果
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
修改页面布局代码activity_main.xml,在页面上添加一个TextView来显示经纬度信息。
<?xml version="1.0" encoding="utf-8"?>
然后打开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组件 @Override public 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定位信息发生改变时触发,用于更新位置信息 @Override public 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添加权限
添加位置如下
更多相关文章
- Android8.0中获取U盘的各种信息
- Android(安卓)数据持久化
- Android的多媒体信息获取
- Android(安卓)放大镜效果实现原理
- Android热修复原理及实现
- Android程序中任意位置获取Context
- Android中如何获取Bitmap(总结)
- Android(安卓)获取未读未接来电和未读短信数量
- Android通过OMA获得ESE的CPLC