基于Windows Mobile 5.0的GPS應用程序開發
開發平臺:操作系統: 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. 環境的搭建 1) 安裝Visual Stuido 2005 ...
- 作者:wang8712來源:CSDN|2007年02月14日
開發平臺:操作系統: 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. 環境的搭建
1) 安裝Visual Stuido 2005
2) 安裝ActiveSync4.0(或更新版本)
3) 安裝Windows Mobile 5.0 Pocket PC SDK(VS2005默認安裝WM2003SDK,所以需要手動安裝WM5的SDK)
以上所需在網上均可找到下載,安裝過程應該比較簡單,沒有什么復雜的設置所以略過不談.有不明白的可以發E-Mail咨詢.
2. 詳細步驟
1) 啟動VS2005.第一次啟動會提示設置默認開發模式,可以選擇Visual C#.
2) 點擊 [文件]->[新建項目]
如圖:
a) 項目類型:選擇Visual C#à智能設備àWindows Mobile 5.0 Pocket PC(如果沒有該選項則說明沒有安裝WM5.0SDK或者安裝失敗)
b) 模板:選擇設備應用程序即可
c) 輸入名稱,位置,解決方案名稱等信息后點擊確定即可.
1) 點擊[文件]à添加à現有項目
找到..\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.
打開后即可添加到現有項目中.如下圖示:
1) 設置項目依賴性
點擊[項目]->項目依賴性
因為要在TestGPS項目中引用添加的項目,所以TestGPS項目依賴于Microsoft.WindowsMobile.Samples.Location
生成順序自然就是TestGPS在后了.
1) 添加項目引用
點擊à[項目]à添加引用
選擇[項目]頁,選擇當前項目后確定.即可在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; }
}
以上提到這些只是為可能會和我有同樣需求的初學的網友提個醒,免得走彎路.
1. 附參考源代碼:
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;
}
}
}