30 Days of .NET [Windows Mobile Applications] - Day 03: GPS Compass(GPS指南针)

原文见Day 03: GPS Compass

需求

使用GPS信息显示指南针。

实现

GPS指南针就是在UI上显示当前位置的方位(azimuth),GPS receiver输出的NMEA里就包含了这个信息。存放在GPRMC的第八个字段。

$GPRMC,015834,A,3749.8448,S,14459.6697,E,000.0,136.8,120908,011.7,E*63

上述例子中136.8为方位角。基于NMEA的分析,我写过一篇文章.NET Compact Framework下的GPS NMEA data数据分析

作者提出进行GPS的开发有三种选择,1.直接使用串口连接GPS receiver。2.使用OpenNETCF GPS Library。3.使用GPS Intermediate Driver
作者最终选择了GPS Intermediate Driver,这个库支持Windows Mobile 5以上系统。MS宣称GPS Intermediate Driver可以屏蔽所有硬件的差异,说实在,屏蔽了所有硬件的差异意味着这个库只是支持通用的NMEA,厂商的NMEA就不能解析出来了。凡事都是有优缺点,技术选型主要取决于需求。对这个指南针的需求比较简单,只是需要取出方位角信息,使用GPS Intermediate Driver可以提高开发效率。

进行GPS Intermediate Driver的开发,可以从参考Windows Mobile 6的事例代码C:\Program Files\Windows Mobile 6 SDK\Samples\PocketPC\CS\GPS。MS把GPS Intermediate Driver封装到Microsoft.WindowsMobile.Samples.Location里面,进行Compatct Framework的开发,我们只需要使用Microsoft.WindowsMobile.Samples.Location就可以了。

GpsDeviceState device = null;
GpsPosition position 
= null;
Gps gps 
= new Gps();

private void Form1_Load(object sender, System.EventArgs e)
{
    gps.DeviceStateChanged 
+= new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
    gps.LocationChanged 
+= new LocationChangedEventHandler(gps_LocationChanged);
}

private void startGpsMenuItem_Click(object sender, EventArgs e)
{
    
if (!gps.Opened)
    {
        gps.Open();
    }
}

private void stopGpsMenuItem_Click(object sender, EventArgs e)
{
    
if (gps.Opened)
    {
        gps.Close();
    }
}

上面是使用Microsoft.WindowsMobile.Samples.Location的核心代码,GpsDeviceState device定义GPS设备的状态,设备的具体状态可以参考GpsDeviceState.cs里面的enum定义

public enum GpsServiceState : int
{
    Off 
= 0,
    On 
= 1,
    StartingUp 
= 2
    ShuttingDown 
= 3,
    Unloading 
= 4,
    Uninitialized 
= 5,
    Unknown 
= -1
}

GpsPosition position定义了位置信息,其实这个类是大而全的类,把通用的NMEA可以分析的信息全部放到这个类里面。

有类图定义可见,我们不仅仅可以得到方位角信息,而且可以在这个类里面得到经纬度,海拔,UTC时间,速度,卫星状态,误差值等等。

Gps对象表示一台Gps的设备,gps.Open();为打开设备,开始接受GPS信息,gps.Close();为关闭该设备。gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);和gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);表示订阅设备状态变化信息和位置变化信息。从GpsPosition的类定义知道,当位置发生变化,也就是回调gps_LocationChanged的时候,可以取出经纬度,海拔等多方面的信息。因此只要订阅该消息就可以完成几乎所有的GPS开发。

使用假GPS(FakeGPS)设备测试程序


作者提供了一个测试方法,使得没有GPS receiver的设备也可以进行测试,FakeGPS 可以在C:\Program Files\Windows Mobile 6 SDK\Tools\GPS找到。FakeGPS可以参考  施炯 同学的 A Windows Mobile GPS Application Sample – Using Fake GPS

设置Share Folder。

安装FakeGPS.CAB。

配置FakeGPS。


使用FakeGPS测试GpsCompass,请在Window Mobile 6 Professional下测试,我在Window Mobile 6 Classic下测试,打开GPS设备失败。

 

安装程序: gpsCompass.cab

源代码: gpsCompass.zip
 

.NET Compact Framework, WinCE, Windows Mobile开发系列

Jake's Blog in 博客园 -- 精简开发 无线生活

posted @ 2009-05-25 10:55  Jake Lin  阅读(4831)  评论(19编辑  收藏  举报