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中View.post和Handler.post的關(guān)系
這篇文章主要介紹了Android中View.post和Handler.post的關(guān)系,View.post和Handler.post是Android開發(fā)中經(jīng)常使用到的兩個(gè)”post“方法,關(guān)于兩者存在的區(qū)別與聯(lián)系,文章詳細(xì)分析需要的小伙伴可以參考一下2022-06-06
Android實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實(shí)例代碼詳解
今天介紹一下,我在項(xiàng)目開發(fā)過程中,實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色變化的方法,實(shí)現(xiàn)方式是,通過隱藏系統(tǒng)的狀態(tài)欄和虛擬按鍵的背景,實(shí)現(xiàn)圖片和背景顯示到狀態(tài)欄和虛擬按鍵下方,需要的朋友可以參考下2019-05-05
Android實(shí)現(xiàn)Tab切換界面功能詳解
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)Tab切換界面的功能,以及對Tab變化事件進(jìn)行監(jiān)聽。文中示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05
android中AutoCompleteTextView的簡單用法(實(shí)現(xiàn)搜索歷史)
本篇文章主要介紹了android中AutoCompleteTextView的簡單用法(自動提示),有需要的可以了解一下。2016-11-11
Android 獲取瀏覽器當(dāng)前分享頁面的截屏示例
本篇文章主要介紹了Android 獲取瀏覽器當(dāng)前分享頁面的截屏示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
Android二級緩存加載圖片實(shí)現(xiàn)照片墻功能
這篇文章主要為大家詳細(xì)介紹了Android二級緩存加載圖片實(shí)現(xiàn)照片墻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09

