Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)
APP中可能會(huì)遇到一種需求,就是將當(dāng)前所在位置的坐標(biāo)傳到服務(wù)器上,今天我提供三種途徑去獲取經(jīng)緯度坐標(biāo)信息,第一種是通過(guò)Android API來(lái)實(shí)現(xiàn),第二種通過(guò)百度地圖API來(lái)實(shí)現(xiàn),第三種通過(guò)天地圖API來(lái)實(shí)現(xiàn)。
第一種方法(Android API實(shí)現(xiàn)),廢話不多說(shuō),上代碼。
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已打開(kāi) } 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)在可用、暫時(shí)不可用和無(wú)服務(wù)三個(gè)狀態(tài)直接切換時(shí)觸發(fā)此函數(shù) @Override public void onStatusChanged(String provider, int status, Bundle extras) { } // Provider被enable時(shí)觸發(fā)此函數(shù),比如GPS被打開(kāi) @Override public void onProviderEnabled(String provider) { Log.e(TAG, provider); } // Provider被disable時(shí)觸發(fā)此函數(shù),比如GPS被關(guān)閉 @Override public void onProviderDisabled(String provider) { Log.e(TAG, provider); } // 當(dāng)坐標(biāo)改變時(shí)觸發(fā)此函數(shù),如果Provider傳進(jìn)相同的坐標(biāo),它就不會(huì)被觸發(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(); // 緯度 } } }; /* * * 打開(kāi)和關(guān)閉gps第二種方法 * private void openGPSSettings() { //獲取GPS現(xiàn)在的狀態(tài)(打開(kā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 { //打開(kāi)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>
運(yùn)行結(jié)果如下
第二種方法(百度地圖API實(shí)現(xiàn),注:需要自己申請(qǐng)apikey)
第三種方法(天地圖API實(shí)現(xiàn))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android通過(guò)原生方式獲取經(jīng)緯度與城市信息的方法
- Android通過(guò)原生APi獲取所在位置的經(jīng)緯度
- Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢(xún)地址并對(duì)獲取的json數(shù)據(jù)進(jìn)行解析的方法
- Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)
- android通過(guò)gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- Android獲取經(jīng)緯度計(jì)算距離介紹
- Android 通過(guò)當(dāng)前經(jīng)緯度獲得城市的實(shí)例代碼
- android手機(jī)獲取gps和基站的經(jīng)緯度地址實(shí)現(xiàn)代碼
- Android獲取經(jīng)緯度的完美解決方案
相關(guān)文章
Android4.4+ 實(shí)現(xiàn)半透明狀態(tài)欄(Translucent Bars)
這篇文章主要為大家詳細(xì)介紹了Android4.4+ 實(shí)現(xiàn)半透明狀態(tài)欄,對(duì)狀態(tài)欄(Status Bar)和下方導(dǎo)航欄(Navigation Bar)進(jìn)行半透明處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Android中將一個(gè)圖片切割成多個(gè)圖片的實(shí)現(xiàn)方法
有種場(chǎng)景,我們想將一個(gè)圖片切割成多個(gè)圖片。比如我們?cè)陂_(kāi)發(fā)一個(gè)拼圖的游戲,就首先要對(duì)圖片進(jìn)行切割2013-05-05linphone-sdk-android版本號(hào)生成解析
這篇文章主要為大家介紹了linphone-sdk-android版本號(hào)生成解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android GridView擴(kuò)展仿微信微博發(fā)圖動(dòng)態(tài)添加刪除圖片功能
這篇文章主要為大家詳細(xì)介紹了Android GridView擴(kuò)展仿微信微博發(fā)圖動(dòng)態(tài)添加刪除圖片功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android實(shí)現(xiàn)可以展開(kāi)的TextView
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可以展開(kāi)的TextView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android自定義相機(jī)界面的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義相機(jī)界面的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android仿騰訊QQ實(shí)現(xiàn)滑動(dòng)刪除 附源碼下載
仿騰訊QQ滑動(dòng)刪除操作,這篇文章主要為大家詳細(xì)介紹了ListView滑動(dòng)刪除的具體操作方法,感興趣的小伙伴們可以參考一下2016-07-07Android開(kāi)發(fā)-之環(huán)境的搭建(圖文詳解)
這篇文章主要介紹了Android開(kāi)發(fā)-之環(huán)境的搭建(圖文詳解),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-11-11Android ScrollView 下嵌套 ListView 或 GridView出現(xiàn)問(wèn)題解決辦法
這篇文章主要介紹了ScrollView 下嵌套 ListView 或 GridView 會(huì)發(fā)列表現(xiàn)數(shù)據(jù)只能顯示一行。因?yàn)樗麄兌际菨L動(dòng)結(jié)構(gòu),兩個(gè)滾動(dòng)條放到一起就會(huì)引起沖突,這里提供解決辦法相關(guān)資料,需要的朋友可以參考下2017-07-07