android获取经纬度

运行效果图:

manifests:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.mingrisoft.getgps">
 4 
 5     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 6     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
 7 
 8     <application
 9         android:allowBackup="true"
10         android:icon="@mipmap/ic_launcher"
11         android:label="@string/app_name"
12         android:roundIcon="@mipmap/ic_launcher_round"
13         android:supportsRtl="true"
14         android:theme="@style/AppTheme">
15         <activity android:name=".MainActivity">
16             <intent-filter>
17                 <action android:name="android.intent.action.MAIN" />
18 
19                 <category android:name="android.intent.category.LAUNCHER" />
20             </intent-filter>
21         </activity>
22     </application>
23 
24 </manifest>

activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <TextView
10         android:id="@+id/location"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="Hello World!"
14         app:layout_constraintBottom_toBottomOf="parent"
15         app:layout_constraintLeft_toLeftOf="parent"
16         app:layout_constraintRight_toRightOf="parent"
17         app:layout_constraintTop_toTopOf="parent" />
18 
19 </android.support.constraint.ConstraintLayout>
MainActivity:
  1 package com.mingrisoft.getgps;
  2 
  3 import android.Manifest;
  4 import android.app.Activity;
  5 import android.content.pm.PackageManager;
  6 import android.location.Location;
  7 import android.location.LocationListener;
  8 import android.location.LocationManager;
  9 import android.os.Build;
 10 import android.support.v4.app.ActivityCompat;
 11 import android.support.v4.content.ContextCompat;
 12 import android.support.v7.app.AppCompatActivity;
 13 import android.os.Bundle;
 14 import android.view.WindowManager;
 15 import android.widget.TextView;
 16 import android.widget.Toast;
 17 
 18 import java.text.SimpleDateFormat;
 19 import java.util.Date;
 20 
 21 public class MainActivity extends Activity {
 22     private TextView text;  //定义用于显示LocationProvider的TextView组件
 23 
 24     @Override
 25     public void onCreate(Bundle savedInstanceState) {
 26         super.onCreate(savedInstanceState);
 27         setContentView(R.layout.activity_main);
 28         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 29                 WindowManager.LayoutParams.FLAG_FULLSCREEN);   //设置全屏显示
 30 
 31         text = (TextView) findViewById(R.id.location);  //获取显示Location信息的TextView组件
 32         //获取系统的LocationManager对象
 33         LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
 34 
 35         // 要申请的权限 数组 可以同时申请多个权限
 36         String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION};
 37         if (Build.VERSION.SDK_INT >= 23) {
 38             //如果超过6.0才需要动态权限,否则不需要动态权限
 39             //如果同时申请多个权限,可以for循环遍历
 40             int check = ContextCompat.checkSelfPermission(this, permissions[0]);
 41             // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
 42             if (!(check == PackageManager.PERMISSION_GRANTED)) {
 43                 //写入你需要权限才能使用的方法
 44                 Toast.makeText(MainActivity.this, "请开启定位权限和GPS!", Toast.LENGTH_LONG).show();
 45             }
 46         }
 47 
 48             //添加权限检查
 49         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
 50             // TODO: Consider calling
 51             //    ActivityCompat#requestPermissions
 52             // here to request the missing permissions, and then overriding
 53             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
 54             //                                          int[] grantResults)
 55             // to handle the case where the user grants the permission. See the documentation
 56             // for ActivityCompat#requestPermissions for more details.
 57             return;
 58         }
 59         //设置每一秒获取一次location信息
 60         locationManager.requestLocationUpdates(
 61                 LocationManager.GPS_PROVIDER,      //GPS定位提供者
 62                 1000,       //更新数据时间为1秒
 63                 1,      //位置间隔为1米
 64                 //位置监听器
 65                 new LocationListener() {  //GPS定位信息发生改变时触发,用于更新位置信息
 66 
 67                     @Override
 68                     public void onLocationChanged(Location location) {
 69                         //GPS信息发生改变时,更新位置
 70                         locationUpdates(location);
 71                     }
 72 
 73                     @Override
 74                     //位置状态发生改变时触发
 75                     public void onStatusChanged(String provider, int status, Bundle extras) {
 76                     }
 77 
 78                     @Override
 79                     //定位提供者启动时触发
 80                     public void onProviderEnabled(String provider) {
 81                     }
 82 
 83                     @Override
 84                     //定位提供者关闭时触发
 85                     public void onProviderDisabled(String provider) {
 86                     }
 87                 });
 88         //从GPS获取最新的定位信息
 89         Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
 90         locationUpdates(location);    //将最新的定位信息传递给创建的locationUpdates()方法中
 91     }
 92 
 93     public void locationUpdates (Location location){  //获取指定的查询信息
 94         //如果location不为空时
 95         if (location != null) {
 96             StringBuilder stringBuilder = new StringBuilder();        //使用StringBuilder保存数据
 97             //获取经度、纬度、等属性值
 98             stringBuilder.append("您的位置信息:
");
 99             stringBuilder.append("经度:");
100             stringBuilder.append(location.getLongitude());
101             stringBuilder.append("
纬度:");
102             stringBuilder.append(location.getLatitude());
103             text.setText(stringBuilder);            //显示获取的信息
104         } else {
105             //否则输出空信息
106             text.setText("没有获取到GPS信息");
107         }
108     }
109 }
原文地址:https://www.cnblogs.com/hemeiwolong/p/12809603.html