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"/>

    添加位置如下

  • 相关阅读:
    编译原理-第二章 一个简单的语法指导编译器-2.4 语法制导翻译
    编译原理-第二章 一个简单的语法指导编译器-2.3 语法定义
    编译原理-第二章 一个简单的语法指导编译器-2.2 词法分析
    LeetCode 1347. Minimum Number of Steps to Make Two Strings Anagram
    LeetCode 1348. Tweet Counts Per Frequency
    1349. Maximum Students Taking Exam(DP,状态压缩)
    LeetCode 1345. Jump Game IV(BFS)
    LeetCode 212. Word Search II
    LeetCode 188. Best Time to Buy and Sell Stock IV (动态规划)
    LeetCode 187. Repeated DNA Sequences(位运算,hash)
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/12520779.html
Copyright © 2011-2022 走看看