您的位置:首页 > 其它

基于Windows Mobile 5.0的GPS应用程序开发

2010-11-16 20:51 549 查看
摘要:

本文从一个完全没接触过移动平台开发的新手的角度讲解基于Windows
Mobile 5.0平台的GPS应用程序的开发过程.体验使用Visual C#开发移动应用程序的高效率.

发平台:
操作系统: Window XP
开发环境:
Visual Studio 2005
Windows Mobile 5.0 Pocket
PC SDK
.Net Compact Framework 2.0 (VS2005自带)
ActiveSync 4.0
移动设备:
Dell
X51 PDA + GPS 卡

环境的搭建

1) 安装Visual Stuido 2005
2)
安装ActiveSync4.0(或更新版本)
3) 安装Windows Mobile
5.0 Pocket PC SDK(VS2005默认安装WM2003SDK,所以需要手动安装WM5的SDK)
以上所需在网上均可找到下载,安装过程应该比较简单,没有什么复杂的设置所以略过不谈.有不明白的可以发E-Mail咨询.


细步骤

1)
启动VS2005.第一次启动会提示设置默认开发模式,可以选择Visual C#.
2) 点击
[文件]->[新建项目]

如图:



a) 项目类型:选择Visual C#à智能设备àWindows Mobile 5.0 Pocket
PC(如果没有该选项则说明没有安装WM5.0SDK或者安装失败)
b)
模板:选择设备应用程序即可
c) 输入名称,位置,解决方案名称等信息后点击确定即可.
3) 点击[文件]à添加à现有项目
找到../Program
Files/Windows CE Tools/wce500/Windows Mobile 5.0 Pocket PC
SDK/Samples/Cs/Gps即可找到Microsoft.WindowMobile.Samples.Location.csproj项目文
件,



该项目封装了访问GPS硬件的一些API函数.使用非常方便.没有找到该文件的话请确认是否安装了WM5.0SDK.
打开后即可添加到现有项目中.如下图示:



4) 设置项目依赖性
点击[项目]->项目依赖性
因为要在TestGPS项目中引用添加的项目,所以TestGPS项目依赖于
Microsoft.WindowsMobile.Samples.Location



生成顺序自然就是TestGPS在后了.



5) 添加项目引用
点击[项目]添加引用



选择[项目]页,选择当前项目后确定.即可在TestGPS项目的引用列表中看到对该项目的引用.



添加对项目类包的引用.



6)
说明

在引入了类包之后,我们就可以在程序中引用已经封装好的类来访问GPS了.在项目中我们可以看到常用的几个类:

DegreesMinutesSeconds.cs
//主要负责经纬度坐标度分秒的转换

DeviceStateChangedEventArgs.cs
//GPS设备状态改变时触发的事件

GPS.cs
//操作GPS的类,主要有负责Open()和Close()GPS设备.

GpsDeviceState.cs //GPS设备的几种状态

GpsPosition.cs
//处理经纬度坐标的类.

LocationChangedEventArgs.cs //位置改变时触发的事件(即经纬度坐标发生变化)

需要说明的是,我在使用GpsPosition类的Longitude和Latitude属性获取经纬度坐标的时候总是出现
DividedByZeroException的例外.经过观察发现是由于对度分秒格式的经纬度坐标值转化为Decimal
Degrees表达形式的时候出错(看了代码之后大家理解的会比我说的更明白,所以看不明白这一点的不必介意因为我的表述也不是很清楚!),而我需要的其
实就是最原始的double类型的经纬度坐标值,不需要进行任何转换即可.所以我对GpsPosition类进行了简单的修改以满足我的需要.


GpsPositon类的末尾加入一下几行代码.

public

double
DoubleLatitude

{

get
{ return
dblLatitude;
}

}

public
double
DoubleLongtitude

{

get
{ return
dblLongitude;
}

}

以上提到这些只是为可能会和我有同样需求的初学的网友提个醒,免得走弯路.


参考源代码:

using System;

using
System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using
Microsoft.WindowsMobile.Samples.Location;

namespace TestGPS

{

public partial class
Form1 : Form

{

//GPS设备状态对象

GpsDeviceState
device = null;

//GPS位置对象

GpsPosition position
= null;

//GPS对象

Gps gps = new Gps();

public Form1()

{

InitializeComponent();

}

private void
Form1_Load(object sender, EventArgs e)

{

gps.DeviceStateChanged += new
DeviceStateChangedEventHandler(gps_DeviceStateChanged);

gps.LocationChanged += new
LocationChangedEventHandler(gps_LocationChanged);

}

//位置改变时更新坐标数据

protected void
gps_LocationChanged(object sender, LocationChangedEventArgs args)

{

position =
args.Position;

}

//gps设备状态改变时更新设备的状态

void
gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)

{

device =
args.DeviceState;

}

//菜单:打开GPS设备

private void
open_gps_Click(object sender, EventArgs e)

{

if (!gps.Opened)

{

gps.Open();

}

open_gps.Enabled
= false;

close_gps.Enabled = true;

}

//菜单:关闭GPS设备

private void
close_gps_Click(object sender, EventArgs e)

{

if (gps.Opened)

{

gps.Close();

}

open_gps.Enabled = true;

close_gps.Enabled = false;

}

//菜单:退出

private void
exit_Click(object sender, EventArgs e)

{

if (gps.Opened)

gps.Close();

Close();

}

//获取经纬度坐标值

private void
getdata_Click(object sender, EventArgs e)

{

string str;

if (!gps.Opened)

{

str =
"GPS设备没有打开,请点击打开GPS菜单后重试!";

MessageBox.Show(str);

return;

}

if (device ==
null)

{

str =
"GPS设备打开错误,请重新插拔GPS卡后重试!";

MessageBox.Show(str);

return;

}

if (position !=
null)

{

string
strJd; //经度

string strWd; //纬度

strJd =
position.DoubleLongtitude.ToString();

strWd =
position.DoubleLatitude.ToString();

DialogResult
result;

str = "经度:"
+strJd + "/n纬度:" + strWd;

result =
MessageBox.Show(str, "当前坐标", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

if (result
== DialogResult.OK)

{

//将经过确认的坐标数据显示到相应的TextBox

wd.Text =
strWd;

jd.Text =
strJd;

return;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: