溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)

發(fā)布時間:2020-09-02 19:24:02 來源:腳本之家 閱讀:223 作者:小小小青年 欄目:移動開發(fā)

APP中可能會遇到一種需求,就是將當(dāng)前所在位置的坐標(biāo)傳到服務(wù)器上,今天我提供三種途徑去獲取經(jīng)緯度坐標(biāo)信息,第一種是通過Android API來實現(xiàn),第二種通過百度地圖API來實現(xiàn),第三種通過天地圖API來實現(xiàn)。

第一種方法(Android API實現(xiàn)),廢話不多說,上代碼。

MainActivity代碼如下:

public class MainActivity extends Activity {
 private static final String TAG = MainActivity.class.getSimpleName();
 private double latitude = 0.0;
 private double longitude = 0.0;
 private TextView info;
 private LocationManager locationManager;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 info = (TextView) findViewById(R.id.tv);
 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
 getLocation();
 //gps已打開
 } else {
 toggleGPS();
 new Handler() {
 }.postDelayed(new Runnable() {
 @Override
 public void run() {
  getLocation();
 }
 }, 2000);
 
 }
 }
 
 private void toggleGPS() {
 Intent gpsIntent = new Intent();
 gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
 gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
 gpsIntent.setData(Uri.parse("custom:3"));
 try {
 PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
 } catch (CanceledException e) {
 e.printStackTrace();
 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
 Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
 if (location1 != null) {
 latitude = location1.getLatitude(); // 經(jīng)度
 longitude = location1.getLongitude(); // 緯度
 }
 }
 }
 
 private void getLocation() {
 Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
 if (location != null) {
 latitude = location.getLatitude();
 longitude = location.getLongitude();
 } else {
 
 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
 }
 info.setText("緯度:" + latitude + "\n" + "經(jīng)度:" + longitude);
 }
 
 LocationListener locationListener = new LocationListener() {
 // Provider的狀態(tài)在可用、暫時不可用和無服務(wù)三個狀態(tài)直接切換時觸發(fā)此函數(shù)
 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
 }
 
 // Provider被enable時觸發(fā)此函數(shù),比如GPS被打開
 @Override
 public void onProviderEnabled(String provider) {
 Log.e(TAG, provider);
 }
 
 // Provider被disable時觸發(fā)此函數(shù),比如GPS被關(guān)閉
 @Override
 public void onProviderDisabled(String provider) {
 Log.e(TAG, provider);
 }
 
 // 當(dāng)坐標(biāo)改變時觸發(fā)此函數(shù),如果Provider傳進(jìn)相同的坐標(biāo),它就不會被觸發(fā)
 @Override
 public void onLocationChanged(Location location) {
 if (location != null) {
 Log.e("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());
 latitude = location.getLatitude(); // 經(jīng)度
 longitude = location.getLongitude(); // 緯度
 }
 }
 };
 
 /*
 * 
 * 打開和關(guān)閉gps第二種方法
 * private void openGPSSettings() {
 //獲取GPS現(xiàn)在的狀態(tài)(打開或是關(guān)閉狀態(tài))
 boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);
 if (gpsEnabled) {
 //關(guān)閉GPS
 Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, false);
 } else {
 //打開GPS 
 Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
 }
 }*/
}

main.xml布局如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/white"
 android:orientation="vertical" >
 
 <TextView
  android:id="@+id/tv"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="經(jīng)緯度信息:"
  android:textColor="#660000"
  android:textSize="20sp" />
 
</LinearLayout>

清單文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.tqmapdemo"
 android:versionCode="1"
 android:versionName="1.0" >
 <uses-sdk
  android:minSdkVersion="8"
  android:targetSdkVersion="18" />
 
 <!-- 連接互聯(lián)網(wǎng)Internet權(quán)限 -->
 <uses-permission android:name="android.permission.INTERNET" />
 <!-- GPS定位權(quán)限 -->
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 
 <application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@android:style/Theme.Black" >
  <activity
   android:name="com.example.tqmapdemo.MainActivity"
   android:label="@string/app_name" >
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
 
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
 </application>
</manifest>

運行結(jié)果如下

Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)

下載Demo請猛戳

第二種方法(百度地圖API實現(xiàn),注:需要自己申請apikey

下載Demo請猛戳

Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)

第三種方法(天地圖API實現(xiàn))

下載Demo請猛戳

Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI