Android中實(shí)現(xiàn)GPS定位的簡單例子
今天弄了一個(gè)多小時(shí),寫了一個(gè)GPS獲取地理位置代碼的小例子,包括參考了網(wǎng)上的一些代碼,并且對代碼進(jìn)行了一些修改,希望對大家的幫助。具體代碼如下: 要實(shí)用Adnroid平臺的GPS設(shè)備,首先需要添加上權(quán)限,所以需要添加如下權(quán)限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
具體實(shí)現(xiàn)代碼如下:
首先判斷GPS模塊是否存在或者是開啟:
private void openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模塊正常", Toast.LENGTH_SHORT)
.show();
return;
}
Toast.makeText(this, "請開啟GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此為設(shè)置完成后返回到獲取界面
}
如果開啟正常,則會直接進(jìn)入到顯示頁面,如果開啟不正常,則會進(jìn)行到GPS設(shè)置頁面:
獲取代碼如下:
private void getLocation()
{
// 獲取位置管理服務(wù)
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服務(wù)信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = locationManager.getBestProvider(criteria, true); // 獲取GPS信息
Location location = locationManager.getLastKnownLocation(provider); // 通過GPS獲取位置
updateToNewLocation(location);
// 設(shè)置監(jiān)聽器,自動(dòng)更新的最小時(shí)間為間隔N秒(1秒為1*1000,這樣寫主要為了方便)或最小位移變化超過N米
locationManager.requestLocationUpdates(provider, 100 * 1000, 500,
locationListener); }
到這里就可以獲取到地理位置信息了,但是還是要顯示出來,那么就用下面的方法進(jìn)行顯示:
private void updateToNewLocation(Location location) {
TextView tv1;
tv1 = (TextView) this.findViewById(R.id.tv1);
if (location != null) {
double latitude = location.getLatitude();
double longitude= location.getLongitude();
tv1.setText("緯度:" + latitude+ "\n經(jīng)度" + longitude);
} else {
tv1.setText("無法獲取地理信息");
}
}
這樣子就能獲取到當(dāng)前使用者所在的地理位置了,至少如何下地圖上實(shí)現(xiàn),在下面將進(jìn)行獲取,并顯示出來!對參考代碼的人表示感謝!
- Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
- Android GPS定位測試(附效果圖和示例)
- Android實(shí)現(xiàn)GPS定位代碼實(shí)例
- 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編程獲取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)點(diǎn)擊某個(gè)按鈕讓菜單選項(xiàng)從按鈕周圍指定位置彈出
這篇文章主要介紹了基于Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕讓菜單選項(xiàng)從按鈕周圍指定位置彈出的相關(guān)資料,需要的朋友可以參考下2015-12-12
Android RecyclerView 數(shù)據(jù)綁定實(shí)例代碼
本文主要介紹Android RecyclerView 數(shù)據(jù)綁定的資料,這里詳細(xì)說明如何實(shí)現(xiàn) Android RecyclerView的數(shù)據(jù)綁定,并附示例代碼,有需要的小伙伴可以參考下2016-09-09
Android 實(shí)現(xiàn)單線程輪循機(jī)制批量下載圖片
這篇文章主要介紹了Android 單線程輪循機(jī)制批量下載圖片的相關(guān)資料,這里對實(shí)現(xiàn)步驟做了詳細(xì)介紹,需要的朋友可以參考下2017-07-07
Android CardView+ViewPager實(shí)現(xiàn)ViewPager翻頁動(dòng)畫的方法
本篇文章主要介紹了Android CardView+ViewPager實(shí)現(xiàn)ViewPager翻頁動(dòng)畫的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
Android中Service和Activity相互通信示例代碼
在android中Activity負(fù)責(zé)前臺界面展示,service負(fù)責(zé)后臺的需要長期運(yùn)行的任務(wù)。下面這篇文章主要給大家介紹了關(guān)于Android中Service和Activity相互通信的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow實(shí)現(xiàn)左側(cè)彈窗效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android聊天工具基于socket實(shí)現(xiàn)
這篇文章主要介紹了基于socket實(shí)現(xiàn)的一個(gè)簡單的Android聊天工具,實(shí)現(xiàn)方法簡單,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2016-02-02

