Android中获取定位经纬度信息
生活随笔
收集整理的這篇文章主要介紹了
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"><TextViewandroid: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組件@Overridepublic 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定位信息發生改變時觸發,用于更新位置信息@Overridepublic 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添加權限
??? <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>添加位置如下
?
?
?
總結
以上是生活随笔為你收集整理的Android中获取定位经纬度信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中使用Jedis连接Redis对
- 下一篇: Java中使用Jedis连接Redis对