卫星数量有两种:卫星总数(all),和已校准卫星数(fixed);
卫星数量来源于gps定位数据,gps数据来源又有两种:内置gps和外接gps设备。
一、内置gps数据来源的计算方式
-
开始启用内置gps时,通过
LocationManager.addGpsStatusListener()
api,监听gps状态,在监听的回调中计算卫星数量,代码:mGpsStatusCallback = new GpsStatus.Listener() { @Override public void onGpsStatusChanged(int event) { if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) { //卫星状态改变 fetchCurGpsStatus(); } } }; mService = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); mService.addGpsStatusListener(mGpsStatusCallback);
-
注册监听永远要记得在不再需要时解除监听,当关闭内置gps时,移除监听:
mService.removeGpsStatusListener(mGpsStatusCallback);
-
卫星状态改变时,首先获取
GpsStatus
,通过如下方式处理:1,通过
GpsStatus
获取到卫星对象(GpsSatellite
)列表,包含了当前能搜到的所有卫星;2,
GpsSatellite
对象中有mUsedInFix
字段标记其是否是参与定位校准。遍历上述列表,统计已校准的卫星数fixedSatellites
,发布消息,通知卫星数量改变;3,基于3卫星定位的最低要求,判断fixedSatellites是否是3颗以上,如果不是,那么认为当前定位失败。
大致代码如下:
private void fetchCurGpsStatus() { GpsStatus mStatus = mService.getGpsStatus(null); //获取卫星颗数的默认最大值 int maxSatellites = mStatus.getMaxSatellites(); //创建一个迭代器保存所有卫星 Iterator<GpsSatellite> iters = mStatus.getSatellites().iterator(); //卫星数 int count = 0; if (iters != null) { while (iters.hasNext() && count <= maxSatellites) { GpsSatellite s = iters.next(); if (s.usedInFix()) { count++; } } } if (count < 3) { //定位失败 } else { //定位成功 } }
二、外接gps设备据来源的计算方式(蓝牙gps)
-
标准nmea格式的gps数据中
GPGGA
格式的数据会包含Number of satellites being tracked(追踪到的卫星数量)
即为目标数据(fixed)。/* $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47 */ Where: GGA Global Positioning System Fix Data 123519 Fix taken at 12:35:19 UTC 4807.038,N Latitude 48 deg 07.038' N 01131.000,E Longitude 11 deg 31.000' E 1 Fix quality: 08 Number of satellites being tracked 0.9 Horizontal dilution of position 545.4,M Altitude, Meters, above mean sea level 46.9,M Height of geoid (mean sea level) above WGS84 ellipsoid (empty field) time in seconds since last DGPS update (empty field) DGPS station ID number *47 the checksum data, always begins with *
-
GPGSV
格式的数据中会包含Number of satellites in view(所有可及的卫星数)
,是可搜到的所有卫星数(all)。/* $GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75 */ Where: GSV Satellites in view 2 Number of sentences for full data 1 sentence 1 of 2 08 Number of satellites in view 01 Satellite PRN number 40 Elevation, degrees 083 Azimuth, degrees 46 SNR - higher is better for up to 4 satellites per sentence *75 the checksum data, always begins with * Sometimes got $GPGSV,1,1,00*75 when just started or inside building.