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("您的位置信息:\n");
 99             stringBuilder.append("經度:");
100             stringBuilder.append(location.getLongitude());
101             stringBuilder.append("\n緯度:");
102             stringBuilder.append(location.getLatitude());
103             text.setText(stringBuilder);            //顯示獲取的信息
104         } else {
105             //否則輸出空信息
106             text.setText("沒有獲取到GPS信息");
107         }
108     }
109 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM