zoukankan      html  css  js  c++  java
  • Android中获取定位经纬度信息

    场景

    根据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">
    
    
        <TextView
            android: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组件
    
        @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("您的位置信息:
    ");
                stringBuilder.append("经度:");
                stringBuilder.append(location.getLongitude());
                stringBuilder.append("
    纬度:");
                stringBuilder.append(location.getLatitude());
    //            stringBuilder.append("
    精确度:");
    //            stringBuilder.append(location.getAccuracy());
    //            stringBuilder.append("
    高度:");
    //            stringBuilder.append(location.getAltitude());
    //            stringBuilder.append("
    方向:");
    //            stringBuilder.append(location.getBearing());
    //            stringBuilder.append("
    速度:");
    //            stringBuilder.append(location.getSpeed());
    //            stringBuilder.append("
    时间:");
    //            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"/>

    添加位置如下

  • 相关阅读:
    用iptables封杀内网的bt软件
    FreeBSD 利用IPFW实现限制局域网使用QQ
    网络安全设备Bypass功能介绍及分析
    活用Windows Server 2008系统的几种安全功能
    恢复mysql管理员密码
    远程控制Windows2003下安装Pcanywhere导致Awgina.dll出错的解决办法
    Ubuntu 11.04 LAMP+JSP环境安装过程
    hbase首次导入大批次的数据成功!
    Chubby是什么?
    DP-Triangle
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/12520779.html
Copyright © 2011-2022 走看看