今天因?yàn)楣ぷ餍枰岩郧熬帉懙囊粋€(gè)gps測(cè)試程序拿出來重新修改了一下。這個(gè)程序說起來有些歷史了,是我11年編寫的,那時(shí)候?qū)W了android開發(fā)沒多久,算是一個(gè)實(shí)驗(yàn)性的作品。現(xiàn)在工作需要,重新拿出來修整。同時(shí)發(fā)現(xiàn)我對(duì)android的gps服務(wù)了解并不深,所以今天特意閱讀了有關(guān)gps服務(wù)的一些資料,把相關(guān)知識(shí)點(diǎn)記錄下來。
本人做了gps相關(guān)的嵌入式軟件已經(jīng)幾年了,所以說起要做個(gè)測(cè)試gps定位模塊的程序,第一反應(yīng)就是串口讀取gps模塊的數(shù)據(jù),然后解析gps的nmea格式數(shù)據(jù)。nmea是一種標(biāo)準(zhǔn)化數(shù)據(jù)格式,不僅僅gps上應(yīng)用了,其他一些工業(yè)通信也是使用這種標(biāo)準(zhǔn)化數(shù)據(jù)格式。解析相關(guān)數(shù)據(jù)然后顯示出來,就完成了一個(gè)基本的gps定位測(cè)試功能。
查了一下才發(fā)現(xiàn)android上做gps相關(guān)定位服務(wù),不需要讀取nmea數(shù)據(jù)分析,android已經(jīng)封裝好了相關(guān)服務(wù),你要做的就是調(diào)用api。這個(gè)不知道應(yīng)該覺得爽還是覺得糾結(jié)。(android也提供了讀取nmea接口,下面會(huì)說到)
1、android 定位服務(wù)
下面我們先來看看android有關(guān)定位服務(wù)提供的支持:
android定位服務(wù)都是位于location下,上面都有相關(guān)說明,這里就不詳細(xì)解析。有一點(diǎn)有需要說說的
是:gpsstatus.nmealistener 官方的說法是可以讀取nmea數(shù)據(jù),但是我這里測(cè)試發(fā)現(xiàn),并沒有讀取到nmea的數(shù)據(jù)。查閱過一些資料,說是google在底層并沒有實(shí)現(xiàn)數(shù)據(jù)反饋的功能。有時(shí)間,需要查看一下源碼。
2、locationmanager定位
//獲取定位服務(wù)
locationmanager locationmanager = (locationmanager) this.getsystemservice(context.location_service);
//判斷是否已經(jīng)打開gps模塊
if (locationmanager.isproviderenabled(android.location.locationmanager.gps_provider))
{
//gps模塊打開,可以定位操作
}
// 通過gps定位
string locatetype= locationmanager.gps_provider;
location location = locationmanager.getlastknownlocation(locatetype);
// 設(shè)置監(jiān)聽器,設(shè)置自動(dòng)更新間隔這里設(shè)置1000ms,移動(dòng)距離:0米。
locationmanager.requestlocationupdates(provider, 1000, 0, locationlistener);
// 設(shè)置狀態(tài)監(jiān)聽回調(diào)函數(shù)。statuslistener是監(jiān)聽的回調(diào)函數(shù)。
locationmanager.addgpsstatuslistener(statuslistener);
//另外給出 通過network定位設(shè)置
string locatetype= locationmanager.network_provider;
location location = locationmanager.getlastknownlocation(locatetype);
3、gpsstatus監(jiān)聽器
上面給出了定位服務(wù)的初始化設(shè)置步驟,但我們都知道gps衛(wèi)星是定期廣播數(shù)據(jù)的,也就是說會(huì)定期收到衛(wèi)星的gps數(shù)據(jù)。我們并不能跟衛(wèi)星主動(dòng)申請(qǐng)數(shù)據(jù),只能被動(dòng)接收數(shù)據(jù)。(中國(guó)的北斗2倒是可以發(fā)送衛(wèi)星報(bào)文給衛(wèi)星)因此我們需要注冊(cè)一個(gè)監(jiān)聽器來處理衛(wèi)星返回的數(shù)據(jù)。
private final gpsstatus.listener statuslistener = new gpsstatus.listener()
{
public void ongpsstatuschanged(int event)
{
// gps狀態(tài)變化時(shí)的回調(diào),獲取當(dāng)前狀態(tài)
gpsstatus status = locationmanager.getgpsstatus(null);
//自己編寫的方法,獲取衛(wèi)星狀態(tài)相關(guān)數(shù)據(jù)
getgpsstatus(event, status);
}
};
4、獲取搜索到的衛(wèi)星
private void getgpsstatus(int event, gpsstatus status)
{
log.d(tag, "enter the updategpsstatus()");
if (status == null)
{
}
else if (event == gpsstatus.gps_event_satellite_status)
{
//獲取最大的衛(wèi)星數(shù)(這個(gè)只是一個(gè)預(yù)設(shè)值)
int maxsatellites = status.getmaxsatellites();
iterator<gpssatellite> it = status.getsatellites().iterator();
numsatellitelist.clear();
//記錄實(shí)際的衛(wèi)星數(shù)目
int count = 0;
while (it.hasnext() && count <= maxsatellites)
{
//保存衛(wèi)星的數(shù)據(jù)到一個(gè)隊(duì)列,用于刷新界面
gpssatellite s = it.next();
numsatellitelist.add(s);
count++;
log.d(tag, "updategpsstatus----count="+count);
}
msatellitenum = numsatellitelist.size();
}
else if(event==gpsstatus.gps_event_started)
{
//定位啟動(dòng)
}
else if(event==gpsstatus.gps_event_stopped)
{
//定位結(jié)束
}
}
上面就是從狀態(tài)值里面獲取搜索到的衛(wèi)星數(shù)目,主要是通過status.getsatellites()實(shí)現(xiàn)。獲取到的gpssatellite對(duì)象,
保存到一個(gè)隊(duì)列里面,用于后面刷新界面。上面是獲取gps狀態(tài)監(jiān)聽器,除了gps狀態(tài)外,我們還需要監(jiān)聽一個(gè)服務(wù),
就是:locationlistener,定位監(jiān)聽器,監(jiān)聽位置的變化。這個(gè)對(duì)做定位服務(wù)的應(yīng)用來說,十分重要。
5、locationlistener監(jiān)聽器
private final locationlistener locationlistener = new locationlistener()
{
public void onlocationchanged(location location)
{
//當(dāng)坐標(biāo)改變時(shí)觸發(fā)此函數(shù),如果provider傳進(jìn)相同的坐標(biāo),它就不會(huì)被觸發(fā)
updatetonewlocation(location);
log.d(tag, "locationlistener onlocationchanged");
}
public void onproviderdisabled(string provider)
{
//provider被disable時(shí)觸發(fā)此函數(shù),比如gps被關(guān)閉
log.d(tag, "locationlistener onproviderdisabled");
}
public void onproviderenabled(string provider)
{
// provider被enable時(shí)觸發(fā)此函數(shù),比如gps被打開
log.d(tag, "locationlistener onproviderenabled");
}
public void onstatuschanged(string provider, int status, bundle extras)
{
log.d(tag, "locationlistener onstatuschanged");
// provider的轉(zhuǎn)態(tài)在可用、暫時(shí)不可用和無服務(wù)三個(gè)狀態(tài)直接切換時(shí)觸發(fā)此函數(shù)
if (status == locationprovider.out_of_service || status == locationprovider.temporarily_unavailable) {
}
}
};
位置監(jiān)聽回調(diào)是用來處理gps位置發(fā)生變化的時(shí)候,自動(dòng)回調(diào)的方法,我們可以從這里獲取到當(dāng)前的gps數(shù)據(jù)。另外我們可以通過回調(diào)函數(shù)提供的location參數(shù),獲取gps的地理位置信息,包括經(jīng)緯度、速度、海拔等信息。
6、獲取地理位置信息(經(jīng)緯度、衛(wèi)星數(shù)目、海拔、定位狀態(tài))
//location對(duì)象是從上面定位服務(wù)回調(diào)函數(shù)的參數(shù)獲取。
mlatitude = location.getlatitude(); // 經(jīng)度
mlongitude = location.getlongitude(); // 緯度
maltitude = location.getaltitude(); //海拔
mspeed = location.getspeed(); //速度
mbearing = location.getbearing(); //方向
7、獲取指定衛(wèi)星信息(方向角、高度角、信噪比)
//temggpssatellite就是我們上面保存的搜索到的衛(wèi)星
//方向角
float azimuth = temggpssatellite.getazimuth();
//高度角
float elevation = temggpssatellite.getelevation();
//信噪比
float snr = temggpssatellite.getsnr();
利用方向角、高度角我們可以繪畫出一個(gè)二維圖形,表示衛(wèi)星在地球哪個(gè)方位,信噪比作用更大。一般的衛(wèi)星定位測(cè)試軟件,都提供了信噪比的狀態(tài)圖,這是表示gps模塊搜星能力的代表。
8、繪畫二維衛(wèi)星位置圖
下面是我做的gps測(cè)試的效果圖:
下面給出一個(gè)根據(jù)方向角和高度角,計(jì)算衛(wèi)星二維圖里面位置的方法,上面效果圖左邊的綠色圓點(diǎn)就代表衛(wèi)星位置。
右邊的信噪比柱狀圖,代表衛(wèi)星的接收信號(hào)能力。
//根據(jù)方向角和高度角計(jì)算出,衛(wèi)星顯示的位置
point point = new point();
int x = mearthheartx; //左邊地球圓形的圓心位置x坐標(biāo)
int y = mearthhearty; //左邊地球圓形的圓心位置y坐標(biāo)
int r = mearthr;
x+=(int)((r*elevation*math.sin(math.pi*azimuth/180)/90));
y-=(int)((r*elevation*math.cos(math.pi*azimuth/180)/90));
point.x = x;
point.y = y;
//point就是你需要繪畫衛(wèi)星圖的起始坐標(biāo)
信噪比的繪畫,就是一個(gè)單位換算,這里就不給代碼了。
9、總結(jié):
android為我們提供了很方便的位置服務(wù),主要通過gpsstatus、locationmanager、gpssatellite這幾個(gè)類實(shí)現(xiàn)相關(guān)服務(wù)和監(jiān)聽。
不過個(gè)人覺得如果能直接讀取nmea的數(shù)據(jù)也是很方便,起碼對(duì)于某些應(yīng)用來說,可以獲取更多信息。