Android實(shí)現(xiàn)GPS定位代碼實(shí)例
通過GPS取得的是一個(gè)Location類型的經(jīng)緯度, 可以轉(zhuǎn)換為兩個(gè)Double 緯度和經(jīng)度.
緯度: 23.223871812820435
緯度: 113.58986039161628
首先創(chuàng)建一個(gè)TextView和兩個(gè)Button
<TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="定位" /> <Button android:id="@+id/btnStop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" />
然后添加主Activity的代碼
Location 是存放經(jīng)緯度的一個(gè)類型
LocationManager是位置管理服務(wù)類型
private Button btnStart; private Button btnStop; private TextView textView; private Location mLocation; private LocationManager mLocationManager; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnStart = (Button)findViewById(R.id.btnStart); btnStop = (Button)findViewById(R.id.btnStop); textView = (TextView)findViewById(R.id.text); btnStart.setOnClickListener(btnClickListener); //開始定位 btnStop.setOnClickListener(btnClickListener); //結(jié)束定位按鈕 } gpsIsOpen是自己寫的查看當(dāng)前GPS是否開啟 getLocation 是自己寫的一個(gè)獲取定位信息的方法 mLocationManager.removeUpdates()是停止當(dāng)前的GPS位置監(jiān)聽 public Button.OnClickListener btnClickListener = new Button.OnClickListener() { public void onClick(View v) { Button btn = (Button)v; if(btn.getId() == R.id.btnStart) { if(!gpsIsOpen()) return; mLocation = getLocation(); if(mLocation != null) textView.setText("維度:" + mLocation.getLatitude() + "\n經(jīng)度:" + mLocation.getLongitude()); else textView.setText("獲取不到數(shù)據(jù)"); } else if(btn.getId() == R.id.btnStop) { mLocationManager.removeUpdates(locationListener); } } }; private boolean gpsIsOpen() { boolean bRet = true; LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Toast.makeText(this, "未開啟GPS", Toast.LENGTH_SHORT).show(); bRet = false; } else { Toast.makeText(this, "GPS已開啟", Toast.LENGTH_SHORT).show(); } return bRet; } 判斷當(dāng)前是否開啟GPS private boolean gpsIsOpen() { boolean bRet = true; LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Toast.makeText(this, "未開啟GPS", Toast.LENGTH_SHORT).show(); bRet = false; } else { Toast.makeText(this, "GPS已開啟", Toast.LENGTH_SHORT).show(); } return bRet; } 該方法獲取當(dāng)前的經(jīng)緯度, 第一次獲取總是null 后面從LocationListener獲取已改變的位置 mLocationManager.requestLocationUpdates()是開啟一個(gè)LocationListener等待位置變化 private Location getLocation() { //獲取位置管理服務(wù) mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); //查找服務(wù)信息 Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); //定位精度: 最高 criteria.setAltitudeRequired(false); //海拔信息:不需要 criteria.setBearingRequired(false); //方位信息: 不需要 criteria.setCostAllowed(true); //是否允許付費(fèi) criteria.setPowerRequirement(Criteria.POWER_LOW); //耗電量: 低功耗 String provider = mLocationManager.getBestProvider(criteria, true); //獲取GPS信息 Location location = mLocationManager.getLastKnownLocation(provider); mLocationManager.requestLocationUpdates(provider, 2000, 5, locationListener); return location; } 改方法是等待GPS位置改變后得到新的經(jīng)緯度 private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // TODO Auto-generated method stub if(location != null) textView.setText("維度:" + location.getLatitude() + "\n經(jīng)度:" + location.getLongitude()); else textView.setText("獲取不到數(shù)據(jù)" + Integer.toString(nCount)); } public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } };
- Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
- Android GPS定位測試(附效果圖和示例)
- android通過gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
- Android實(shí)現(xiàn)Service獲取當(dāng)前位置(GPS+基站)的方法
- android手機(jī)獲取gps和基站的經(jīng)緯度地址實(shí)現(xiàn)代碼
- Android中GPS定位的用法實(shí)例
- Android中實(shí)現(xiàn)GPS定位的簡單例子
- Android編程獲取GPS數(shù)據(jù)的方法詳解
- Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)詳解
- Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)
相關(guān)文章
Android自定義控件(實(shí)現(xiàn)視圖樹繪制指示器)
本文主要介紹了Android視圖樹繪制指示器的實(shí)現(xiàn)原理和具體步驟。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01android LinearLayout 布局實(shí)例代碼
android LinearLayout 布局實(shí)例代碼,需要的朋友可以參考一下2013-04-04Android開發(fā)Jetpack組件Lifecycle原理篇
這一篇文章來介紹Android?Jetpack架構(gòu)組件的Lifecycle;?Lifecycle用于幫助開發(fā)者管理Activity和Fragment?的生命周期,?由于Lifecycle是LiveData和ViewModel的基礎(chǔ);所以需要先學(xué)習(xí)它2022-08-08android實(shí)現(xiàn)多點(diǎn)觸摸效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)多點(diǎn)觸摸效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05刷新Activity中的scrollview示例(局部ui刷新)
代碼很簡單,但是很實(shí)用,適合在一個(gè)Activity中要刷新局部的UI,比如在掃描一維碼的時(shí)候,要把每次掃描的結(jié)果都顯示在界面上2014-01-01Android應(yīng)用自動更新功能實(shí)現(xiàn)的方法
這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用自動更新功能實(shí)現(xiàn)的方法,感興趣的小伙伴們可以參考一下2016-06-06Android實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換
這篇文章主要介紹了Android實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android編程實(shí)現(xiàn)長按彈出選項(xiàng)框View進(jìn)行操作的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)長按彈出選項(xiàng)框View進(jìn)行操作的方法,結(jié)合實(shí)例形式分析了Android事件響應(yīng)及彈窗的功能、布局相關(guān)操作技巧,需要的朋友可以參考下2017-06-06Android ListView出現(xiàn)異常解決辦法
這篇文章主要介紹了Android ListView出現(xiàn)異常ListView:The content of the adapter has changed but ListView did not receive a notification解決辦法的相關(guān)資料,需要的朋友可以參考下2016-11-11